Jump to content

Archived

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

RTS

change uploaded files name

Recommended Posts

I am trying to make my form change 'uploadFile' to the name 'test', then upload the file 'test' to users/images/pictures. at the moment I get this error:[code]
The requested URL /<br /><b>Warning</b>: rename(uploadFile,test): No such file or directory in <b>/Library/WebServer/Documents/edit.php</b> on line <b>205</b><br />Rename failed was not found on this server.[/code]
for this code:
[code]<form enctype='multipart/form-data' action="<?php
$file_old = "uploadFile";
$file_new = "test";

if(!rename($file_old, $file_new))
{
    echo ("Rename failed");
}

move_uploaded_file ($_FILES['test'] ['tmp_name'],
      "users/images/pictures/{$_FILES['uploadFile'] ['name']}")

?>" method='post'>
Picture: <input name='uploadFile' type='file'>
<input type='submit' value='Upload'> [/code]
I am still fairly new to php, so if I am doing this completely the wrong way just tell me.

Share this post


Link to post
Share on other sites
anyone?, or can someone point me in the direction of a script that will do this... or a tutorial?

Share this post


Link to post
Share on other sites
[quote]at the moment I get this error:
Code:

The requested URL /<br /><b>Warning</b>: rename(uploadFile,test): No such file or directory in <b>/Library/WebServer/Doc [/quote]

This is because rename() function is a file function, it's used too rename one filename too another.

[code]<form enctype='multipart/form-data' action="<?php
$file_old = "uploadFile";
$file_new = "test";

if(!rename($file_old, $file_new))
{
    echo ("Rename failed");
}
?>" method='post'>
Picture: <input name='uploadFile' type='file'>
<input type='submit' value='Upload'> [/code]
can you please explain why are you trying too change uploadfile too test? too me it doesn't make any sence?

below is an example of how i would do it you may find that it's not what your after but it may be of benifit too someone else,

form.html
[code]
<form enctype='multipart/form-data' action="upload.php" method="post">
Picture: <input name='uploadFile' type='file'>
<input type='submit' value='Upload'>
</form>
[/code]

upload.php
[code]
if($FILES['uploadFile']['name']){ //A picture was supplied lets move it too the correct dir
if(move_uploaded_file ($_FILES['test'] ['tmp_name'],"users/images/pictures/{$_FILES['uploadFile'] ['name']}"){ //Ok were moving it, but did it get there?
echo "Your picture was successfully uploaded"; //Success the picture was uploaded successfully
}else{
echo "Unable too upload your picture"; //Strangly the picture didn't upload, php should have given you a reason above
}
}else{
echo 'No Picture supplied!'; //They didnt supply a picture
}
[/code]

The example above will check too see if an upload was supplied, if it finds it is it will then move the file too the pictures directory.

hope it helps

Stuie

Share this post


Link to post
Share on other sites
<?php
  $fileHand = fopen('tempFile.txt', 'r');
  fclose($fileHand);
  rename( 'tempFile.txt', 'tempFile2.txt' );
?>

Share this post


Link to post
Share on other sites
I actually dont want it to be 'test' that was just for testing purposes. I really want it to be $username. that way, I can associate the upload with the user that uploaded it.

Share this post


Link to post
Share on other sites

×

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.