I want to let users upload pics to the server but I`m having problem figuring out how to tell $_FILES what the file name should be?
Here is my upload form that I tried to use :
here $photo_id = $user_name.time() .
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="file" name"<?php echo md5($photo_id)?>" />
<input type="submit" value="upload"/>
</form>
Here is the part where I get stuck how do I name the file? I can`t use this way : $_FILES[$_POST[md5($photo_id)]]['name'] as you can see I have the same proble with $_POST giving such a weird expression . I wil开发者_如何学Gol also be updating mysql database with the filename .
Can anyone suggest a better way of naming files so that filesnames don`t clash . If I use a simple file name , it may coincode with some other file of same name and overwrite ? tHANKX!
It doesn't make sense to give the file field's name an altering value.
Give it a constant value:
<input type="file" name="photo" />
If you want to suggest a file name, use a separate hidden field:
<input type="hidden" name="filename" value="<?php echo md5($photo_id)?>">
However note that this could be manipulated by the user. You will probably be better off doing the md5()
later on server side, before actually storing the file - if you want to use it at all, because
Can anyone suggest a better way of naming files so that filesnames don`t clash . If I use a simple file name , it may coincode with some other file of same name and overwrite ?
there is a number of things you can do (there are excellent answers for this on SO already listing all the options and their advantages and disadvantages, but I can't find any of them right now!). One method that is fairly safe is combining the value of the photo's name or the user's ID, and the current microtime()
. The danger of collisions this way is microscopic.
1'st look again at your HTML and correct errors
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="file" name="photo" />
<input type="submit" value="upload"/>
</form>
2'st after submit
<?php
$uploadedFile = $_FILES['photo']['name']; // uploaded file - for db name and md5()
$tmpName = $_FILES['photo']['tmp_name']; // uploaded tmp file
// I assume you are not handling users for web form and you are storing them in a session, if you need additional input on form you can add some hidden values.
$newFile = md5($uploadedFile.time().$_SESSION['user']);
move_uploaded_file($tmpName, $uploads_dir/$newFile);
?>
You only need to use a simple name for the file input:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="file" name"myFile" />
<input type="submit" value="upload"/>
</form>
Then when your form is submitted, the uploaded file can be referenced using $_FILES['myFile']
.
The file will be uploaded to temporary storage, and you then have to use a function such as move_uploaded_file()
to move it to permanent storage.
Something like:
$tmp_name = $_FILES['myFile']['tmp_name'];
$name = $_FILES['myFile']['name'];
move_uploaded_file($tmp_name, 'uploads/' . $name); // Move file to /uploads folder
http://uk.php.net/manual/en/function.move-uploaded-file.php has some more info
Update
If you want to make sure that you're not overwriting files with the same name, use file_exists()
.
if(file_exists($_FILES['myFile']['name']))
{
// File already exists
}
精彩评论