forcing a file name change.

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
shawnlarrabee
Forum Newbie
Posts: 6
Joined: Mon Apr 17, 2006 10:14 pm

forcing a file name change.

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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>';
(#10850)
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

With comments on it

Post 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
Post Reply