View Full Version : I Need Help With Uploading A Image Script
Can someone please write me a good uploading a file in PHP script. I need the script to be able to upload a Image only JPEG format and i want it so that the user can rename the file so instead of it being image00001.jpg they could enter in a text box 26.jpg. I will gratefully appreciate if someone could do this for me.
Thanks Kevin
whitty
03-27-2003, 08:04 PM
http://www.hotscripts.com/PHP/Scripts_and_Programs/File_Manipulation/Upload_Systems/
I need a script that they user can enter in a text box what the new file name will be
Spookster
03-27-2003, 08:44 PM
Ummm if they are uploading their file then shouldn't it already be named what they want? :confused:
No because the file is related a shopping cart i want it so that the file will be renamed to the products ID
Spookster
03-27-2003, 09:06 PM
If the user is going to be uploading the file from their computer and you want the filename to be the product ID then why would you want to allow the user to rename the file?
Well so they could enter the products ID
Can someone just give me the code??
Spookster
03-27-2003, 09:12 PM
Is the prodcut ID already known in the system or does the user make it up right then and there?
If it is already known in the system then don't make the user type it in because people make mistakes too easy. If the product ID is already know then automatically rename it to the proper product ID.
If it is not known then and the user just makes it up just tell the user to name their file accordingly before they upload it.
That would be my suggestion anyways.
It is known in the SQL DB...But the user needs to know the product name and that is in the DB as well but the picture needs to be a number.jpg for my shopping cart system to recgonize that the picture exists. If you have code for that please let me know.
Spookster
03-27-2003, 09:19 PM
Well I am attempting to make this easier for you and steer you away from a potentially error prone system. There's no need to be rude.
If the product information is already stored in the database then simply query the database for the information and use the product ID that you have stored as the new name for the image that the user uploads.
I am not trying to be rude I apolgize if I seem to be. Do you have code that i can have that would do that for me.
Spookster
03-27-2003, 09:35 PM
Code to do what? There is a function in PHP for renaming files:
http://www.php.net/manual/en/function.rename.php
The code that will upload and rename the file to the products ID number
I have this
<html>
<body>
<?php
$file_dir = "/home/virtual/admin8/var/www/html/boo/"; // directory where you want file to be uploaded to
$file_url = "http://207.44.172.156/boo"; //same directory, but as link
foreach( $HTTP_POST_FILES as $file_name => $file_array ) {
if (substr($file_array['type'], 0, 5)=="image") {
echo "path: ".$file_array['tmp_name']."<br>\n";
echo "name: ".$file_array['name']."<br>\n";
echo "type: ".$file_array['type']."<br>\n";
echo "size: ".$file_array['size']."<br>\n";
if ( is_uploaded_file( $file_array['tmp_name'] ) ) {
move_uploaded_file( $file_array['tmp_name'], "$file_dir/".$file_array['name']);
echo "<a href=\"$file_url/$file_name\">".$file_array['name']."</a>\n\n";
}
} else {
echo "File is not an image.";
}
}
?>
<form enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="3000000000000">
<input name="fupload" type="file"><br>
<input type="submit" value="Send File">
</form>
</body>
</html>
But I need the renaming thing..
Ok i got it all to upload I just need it to now rename to whatever the ID is which was selected in the Drop down box here is my code can someone please update me code so that it all works:
<html>
<body>
<?php
require("Cart.php");
Brand();
DBInfo();
Root();
$file_dir = "/home/virtual/admin8/var/www/html/boo/"; // directory where you want file to be uploaded to
$file_url = "http://207.44.172.156/boo"; //same directory, but as link
echo "<select name=\"II\" size=\"1\">";
mysql_connect("$DBHost","$DBUser","$DBPass");
$result=mysql("$DBName","SELECT ItemID,ItemName,ItemSKU FROM Items ORDER BY ItemID");
while ($row = mysql_fetch_row($result)) {
echo "<option value='$row[0]'> $row[1] - $row[0] </option>";
}
echo "</select></td></tr>";
foreach( $HTTP_POST_FILES as $file_name => $file_array ) {
if (substr($file_array['type'], 0, 5)=="image") {
echo "path: ".$file_array['tmp_name']."<br>\n";
echo "name: ".$file_array['name']."<br>\n";
echo "type: ".$file_array['type']."<br>\n";
echo "size: ".$file_array['size']."<br>\n";
if ( is_uploaded_file( $file_array['tmp_name'] ) ) {
move_uploaded_file( $file_array['tmp_name'], "$file_dir/".$file_array['name']);
echo "<a href=\"$file_url/$file_name\">".$file_array['name']."</a>\n\n";
}
} else {
echo "File is not an image.";
}
}
?>
<form enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="3000000000000">
<input name="fupload" type="file"><br>
<input type="submit" value="Send File">
</form>
</body>
</html>
the second parameter of the
move_uploaded_file
call is where you should rename the file, so just sling in your new name reference there rather than the
"$file_dir/".$file_array['name']
bit.
As Spookster was trying to warn - using the client name is so dodgy and will (statistically) land you in trouble with an accidental overwrite at some stage in the future.
I strongly advise taking a few moments to analyse your coding to head off that problem and only use a scripted call to define the saved filename.
As a subnote:
if you only wanted jpeg files and only jpeg files, you could use
$a = getimagesize($_FILES['fupload']['tmp_name']);
if($a[2] !== 2) {// not a valid jpeg file }
I need know how to make the file name what ever the drop down box choose. How can I do that?
The dropdown box contains information from the database. I want the file name to depend on what was choosen in the drop down box. How can I do that?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.