Jump to content

Script to upload and change name


forumnz

Recommended Posts

I have here a script that should allow the user to browse for an image and change the name of it. Then click upload and the image will be uploaded under the new name.

 

The image will upload but the name wont change.

 

<?php


// directory path.

$uploaddir = 'upload_new/';

// get the dir to send file to and the file name.

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

$file_name=$uploadfile;

@rename($file_name,$uplaodfile);

// if all the conditions are correct send the file to the directory.

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)){

// success echoed

echo " <font color='red'>File is valid, and was successfully uploaded.</font>";

}else {

//unsuccesfull echoed

echo "<font color='red'>File was unsuccesful sorry</font>";

}

// show the form.
   
echo"
<form enctype='multipart/form-data' action='upload.php' method='POST'>
   
<input type='hidden' name='MAX_FILE_SIZE' value='30000000000000000000000'>
please provide a file name:
<br>
send this file <input name='file_name' type='text' >
<br>

send this file <input name='userfile' type='file' >
    
<input type='submit' name='submit' value='Send File'>

</form>";

?>

 

 

Thanks,

 

Sam.

Link to comment
https://forums.phpfreaks.com/topic/45915-script-to-upload-and-change-name/
Share on other sites

Why rename the file when you can upload it under the new name?

 

That and your rename function is flawed for a few reasons, first you are calling it on a "non-existent" file. It should be after the move_upload. Second why not just set the name correct in the move_upload file part???

 

And your are renaming the file "$upload_file" to "$filename" which stores the value of "$upload_file" That does not make any sense at all??????

 

I would suggest reading the manual and getting your functions straight

www.php.net/move_uploaded_file

www.php.net/rename

 

I am sure if you look in the user comments for the move_uploaded_file you will find how to rename a file etc.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.