Page 1 of 1

forcing a file name change.

Posted: Tue Apr 25, 2006 10:18 pm
by shawnlarrabee
Form.PHP

Code: Select all

<form action="upload.php" method="post" ENCTYPE="multipart/form-data">
File: <input type="file" name="file" size="30"> <input type="submit" value="Upload!">
</form>

Upload PHP

Code: Select all

<?php
// ==============
// Configuration
// ==============
$uploaddir = "uploads"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
// ==============
// Upload Part
// ==============
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
print "Your file has been uploaded successfully! Yay!";
?>

I found this code on-line and have been using it, without completely understanding everything about it, to upload images to my server. My questions:
1. What do I need to do in order that the new uploaded file will be saved to a name and extension that I have assigned? (force a name change. ie xxxnewuploaded.jpg saves as predeterminedfilename.jpg)
2. I am obviously a noobie, would anyone like to help me understand what the basic elements/structure of the strings in this code are? For example, $_FILES is an array? What is the structure of an array?

My objective: I am building a web-site for a friend who knows nothing about coding, etc. He wants to be able to upload pictures, but I want to control cludder on his server. Therefore- I want each new image to replace the old as the same name.


Thanks!

Posted: Tue Apr 25, 2006 10:33 pm
by feyd
  1. The second argument to move_uploaded_file() is the destination path and file name. This is where you will alter.
  2. Have a read here first. If you have further questions or need some clarification, we'll chime in.

Posted: Tue Apr 25, 2006 10:38 pm
by Christopher
It will help you to understand what is in the $_FILES variable if you check its contents. Put the code below on the upload page to display it:

Code: Select all

echo '<pre>' . print_r($_FILES, 1) . '</pre>';

With comments on it

Posted: Wed Apr 26, 2006 1:58 am
by dibyendrah
I have added the comments in each line so as to make you easier to learn what it does.

Code: Select all

<?php
// ==============
// Configuration
// ==============
$uploaddir = "uploads"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!  <- Destination folder to upload the files

//if you want to change the mode to 0755 i.e; read, write, execute to owner, read write but no execute for group and other user
chmod("uploads", 0755);  // octal; correct value of mode
// ==============
// Upload Part
// ==============
if(is_uploaded_file($_FILES['file']['tmp_name'])) <- Checks if the file is uploaded
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']); //move the uploaded file from temporary directory to the destination folder
}
print "Your file has been uploaded successfully! Yay!";
?>
Hope this will make a clear view of code.

Cheers,
Dibyendra