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!
i have a html form used to upload .jpg files onto my WAMP server. I am trying to validate if the file uploaded already exists in my server. I already have a .jpg file to test against. What i am having trouble is that whenever i upload any .jpg file no matter if the file exists, it seems to go into my success messsage:
$target = 'Shanghai_2010/images/';
$target .= basename($_FILES['photo']['name']);
if(isset($_FILES['photo']))
{
if(!file_exists($target))
{
$error = 'This file already exists on the server';
include 'error.html.php';
exit();
}
else
{
// move file to server
echo 'success this file does not exist on server';
//echo($target);
}
}
can somebody tell me why it always goes into my success message?
That ! before your file_exists() negates the return of file_exists(). So, if file_exists() returns TRUE, that ! changes the return to FALSE, explaining why your if conditional fails.
By removing the ! it still seems to go into my success message. I tried to debug it and placed an echo within the success message and echoed: Shanghai_2010/images/IMG_0075.JPG which i kinda expected, but i didnt' know why it goes into my success message when the file does already exist because i can clearly see it and go to it directly being within Shanghai_2010/images/....
Sounds like your paths aren't correct. Is the file on the server using a lower-case extension (i.e., .jpg as opposed to .JPG)? Not sure if that could be a problem. Is your PHP file in the directory above /Shanghai_2010/?
The file on the server is called, IMG_0075.JPG, when i did the echo, it was the exact match so visually they looked the same. My php file, index.php is within Shanghai_2010 and 'images' folder is also within that. Also i'm using WAMP server on windows machine, the / and \ slashes, do i need to conform to a rule as to how the slahes in my php file should appear? i.e it is currently $target = 'Shanghai_2010/images/'; do i need to change it to \ ? like how it looks on my windows machine?
and when i submitted the form it gave me the following: Array ( [name] => IMG_0075.JPG [type] => image/pjpeg [tmp_name] => C:\wamp\tmp\php810E.tmp [error] => 0 [size] => 63467 )
does that help at all?