move upload confusion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jodeti
Forum Newbie
Posts: 14
Joined: Tue Apr 28, 2009 8:54 am

move upload confusion

Post by jodeti »

Hello,
It may be silly. Is that possible. Trying to upload one file(image file) from the form to a directory with different name.

Code: Select all

$CurrentPath="./upload";
$_POST["ActionIfFileExistsOnUpload"]="Make unique";
if(isset($_FILES["FileToUpload"])&&$_FILES["FileToUpload"]["name"]!="")
    {
        $NewName = $_FILES["FileToUpload"]["name"];
if(file_exists("$CurrentPath/{$_FILES["FileToUpload"]"name"]}")&&$_POST["ActionIfFileExistsOnUpload"]=="Make unique")/*ignore that one it's just try to be a unique*/
        {
        $NewName = md5(uniqid(rand(0, 1000),1))."_".$_FILES["FileToUpload"]["name"];
        $thumb = md5(uniqid(rand(0, 1000),1))."thumb_".$_FILES["FileToUpload"]["name"];
            }  
    move_uploaded_file($_FILES["FileToUpload"]["tmp_name"], "$CurrentPath/$NewName");
    $thumb = "thumb_".$_FILES["FileToUpload"]["name"];/*try to rename*/
    move_uploaded_file($_FILES["FileToUpload"]["tmp_name"], "$CurrentPath/$thumb"); /possible????????/
      }
When try to upload by removing the "below two line" it work usual but when i try to add this two line to upload that file with different name. One is the $_FILES["FileToUpload"]["name"] and the other is the $thumb.

Code: Select all

$thumb = "thumb_".$_FILES["FileToUpload"]["name"];
move_uploaded_file($_FILES["FileToUpload"]["tmp_name"], "$CurrentPath/$thumb");
Adding these two line doesn't effect the script. The script create filename with the first one($NewName).
Is this a overwriting issue or it doesn't work because it move_uploaded_file(I mean try to cut from php temporary to the destination.) That's why those two line has no effect in the script??????????

Any suggestion?

thank you ALL.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: move upload confusion

Post by AbraCadaver »

In line 11 above you have already "moved" the uploaded file from 'tmp_name', so it is no longer there and you can't move it again. Try this on line 13 instead:

Code: Select all

copy("$CurrentPath/$NewName", "$CurrentPath/$thumb");
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
jodeti
Forum Newbie
Posts: 14
Joined: Tue Apr 28, 2009 8:54 am

Re: move upload confusion

Post by jodeti »

It works what i wanted. thank you.
Post Reply