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!
Quick question. I'm working on a file upload system and I've ran into the problem if a files exists on the server. I'm doing a check to see if it exists and if it dose then stops the upload (well move uploaded file in this case).
if ((($_FILES["file1"]["type"] == "image/gif") || ($_FILES["file1"]["type"] == "image/jpeg") ||($_FILES["file1"]["type"] == "image/pjpeg")) && ($_FILES["file1"]["size"] < 100000)){ //check to see if the file uploaded is allowed
if ($_FILES["file1"]["error"] > 0){
echo "<br />There was an error with the file upload: <br />";
echo "Return Code: " . $_FILES["file1"]["error"] . "<br />"; //print error
}else{
if(file_exists("upload/" . $_FILES["file1"]["name"])){ //check to see if the file name already exists in upload folder
echo $_FILES["file1"]["name"] . " already exists. ";
}else{
move_uploaded_file($_FILES["file1"]["tmp_name"],
"upload/" . $_FILES["file1"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file1"]["name"]; //move file to upload dir
}
}
What I'm trying to figure out is there a way to over write it? I was curious if just doing something like moving it anyway after a prompt will overwrite it using "move_uploaded_file()"? Or was there something special I should do?
On a side note if anyone knows off hand, how long will a server by default keep the temp uploaded file before removing it if it was not renamed on the move?
Yeah, I agree with Social there and change the name as it uploads. But the unlink() is good idea for a backup plan too. Might be worth coding something in anyway... lol
Well I'm trying out a few things, I'm going to include an unlink() to 'overwrite' the image in this case but I'm trying to make something that will give the user a way to check if they want to over write it. I'm doing a simple form with a radio button YES/NO, and I'm trying to pull a preview for the uploaded file, but it's not working because I don't know where the file is. Is there a way to pull the temp file as an image with out doing move_uploaded_file() then unlink() it with some extra variables?
So after some minor hair pulling, I figures out something. This little bit of code will check to see if the file is uploaded, if an file already exists, then it will ask if you want to overwrite it with a preview, then if it dose not already exist then it just moves it form the server tmp file.