Jump to content

change uploaded files name


RTS

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.
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.