Page 1 of 1

php validation not working ono $_FILES[]

Posted: Tue Jun 08, 2010 5:32 am
by nvidia123
Hi,

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:

Code: Select all

 <form enctype="multipart/form-data" action=" " method="post">        
        <div>
        <label for="description"> Description: </label> <input type="text" name="description" id="description" /> <br >
        </div>
        
        <div>
            <label for="category"> Category </label>
            <select name="category" id="category">
                    <option value="Cheng_Huang_Temple">Cheng Huang Temple </option>
                    <option value="Shanghai_Zoo">Shanghai Zoo </option>
            </select> <br />
        </div>
        
        <div>
            <label for="uploadimg"> Upload Photo: </label>
         <input type="file" id="photo" name="photo"/> <br >
        </div>
        
        <div>
        <input type="hidden" name="action" value="upload" />
        <input type="submit" value="Upload" />
        </div>
    </form>
php code snippet:

Code: Select all


  $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?

Re: php validation not working ono $_FILES[]

Posted: Tue Jun 08, 2010 5:43 am
by markusn00b
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.

Re: php validation not working ono $_FILES[]

Posted: Tue Jun 08, 2010 5:57 am
by nvidia123
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/....

Any reason why?

Re: php validation not working ono $_FILES[]

Posted: Tue Jun 08, 2010 6:02 am
by markusn00b
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/?

Re: php validation not working ono $_FILES[]

Posted: Tue Jun 08, 2010 6:16 am
by nvidia123
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?

Re: php validation not working ono $_FILES[]

Posted: Tue Jun 08, 2010 6:57 am
by nvidia123
Hi,

i did some debugging and i did the following:

if(isset($_FILES['photo']))
{
print_r ($_FILES['photo']);

// rest of code skiped


}

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?