FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


renaming uploaded image file?





AOP Web Development
hello! i just got a problem in how to rename the uploaded file... just like an image because i want to replace the name of it... may i know how to do it php? Laughing Laughing Laughing Laughing Laughing
Star Wars Fanatic
Well, assuming I understand what you mean, it would be possible to do that with imagecreatefrompng (well, if your image is a png) then just save it using imagepng...

Here is some code that will do that...

Code:
<?php
$im = imagecreatefrompng("oldfilename.png");
imagepng($im,"newfilename.png");
?>
ammonkc
I'm not really sure what you're trying to do, but I'm guessing that your are accepting file uploads or image uploads via a web form. If this is the case this code might help.

Code:

if(is_uploaded_file($_FILES['myFile']['tmp_name'])) {
      if (file_exists($uploaddir.$_FILES['myFile']['name'])) {
         unlink($uploaddir.$_FILES['myFile']['name']);
      }
      move_uploaded_file($_FILES['myFile']['tmp_name'],$uploaddir.str_replace(" ","_",$_FILES['myFile']['name'])) or die("Could not move file");
      $rs_file = $_FILES['myFile']['name'];
}//end if


You were not very clear about your question. maybe a more detailed description of what your are trying to acomplish would help.
AOP Web Development
well i mean after you upload a file from the form you will have to upload it to the server by using to move_uploaded_files() but i just want to know how to rename the uploaded file for the new one to avoid duplications of the files.
kv
The syntax of move_uploaded_file is like this
Code:
bool move_uploaded_file ( string $filename, string $destination )


$destination is the file name that the uploaded file will be moved into. You can always check if it already exists. If it does, then change the value of $destination (append with _1 or whatever) till you get a file name that does not exist. Then call move_uploaded_file.

Also check http://www.php.net/move_uploaded_file
AOP Web Development
I know already how to use it the only problem is the on how to rename the upload image file from the form and before i move to the server i want to rename the file name.
ammonkc
you shouldn't have to rename the file before that. The file is just saved as a temp file until you move it. at least thats how I've understood it, I could be wrong.
AOP Web Development
can you share any code for that?
kv
Code:
$destination="renamedfile.jpg";
move_uploaded_file($_FILES['myFile']['tmp_name'], $destination);
Fire Boar
The above is correct. And if you want a good random name generator, I like this sort of thing...

Code:
<?php
$target_path = "pictures/";
// Generate a random 32-digit hex string
$random = md5(microtime()*rand(100));
while (file_exists('images/'.$random.'.jpg'))
{
   // Failsafe
   $tries++;
   if ($tries == 10) die('Image upload failed: too many retries.');
   // Regenerate and try again
   $random = md5(microtime()*rand(100));
}
$target_path = $target_path.$random.".jpg";
if(!move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
   die ('There was an error uploading the file, please try again.');
}
?>
AOP Web Development
Thanks for sharing your idea.... I will try to use your code.
Related topics
Image Processing in C++ by Shirish
How To Make A Mini-Image for safedisc 4
Help for sending 2+ cookies in one page.
Problems with file manager: + sign
php image exif functions
PNG file Copyright
How to show PNG image transparency in IE?
How can I burn an ISO image larger than 700mb on a 700mb CD?
How to avoid people stealing your images?
How to detect a file is that jpeg image or gif image ?
Uploading and resizing an image
Upload image file problem
Image store in mysql
Interesting mistake - change extension on image
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.