Solved

No file uploaded, but code says it exists

Posted on 2004-08-27
3
246 Views
Last Modified: 2006-11-17
I have a code I'm working on where pictures are uploaded from a page and then outputted on a new page.  My problem is that when no pictures are uploaded, I get red x's.  The code is saying the file exists when it doesn't.

This is the code that handles the image file upload:
[code]
$path_to_file = './images/';
$files = $_FILES['files'];

if (!ereg("/$", $path_to_file))
        $path_to_file = $path_to_file."/";
$i = 0;
$imgname = array();
foreach ($files['name'] as $key=>$name) {
  if ($files['size'][$key]) {
     // clean up file name
    $name = ereg_replace("[^a-z0-9._]", "",
      str_replace(" ", "_",
          str_replace("%20", "_", strtolower($name)
          )
       )
     );
     $location = $path_to_file.$name;
         while (file_exists($location))
        $location .= ".copy";
     copy($files['tmp_name'][$key],$location);
     $imagesToDisplay[] = $location;
   unlink($files['tmp_name'][$key]);
     echo "\n
 Successfully uploaded file: $name.<br>";      
      $imgname[$i++] = $name;

  }
}
}
[/code]

Here is the code where I try to recall the picture back up onto a page.  I took out the image calling for debugging reasons, so instead of red x's I see "Picture Exists."  When I add to the output, and upload a picture ${imgname[0]} works perfectly (output says "Picture Exists and is named image.jpg"). When I don't upload a picture, it is left blank (output says "Picture Exists and is named "). I just don't know why it would say the picture exists when there's nothing there to base that on.

Code:
[code]
if (file_exists("images/${imgname[0]}")) {
print("Picture Exists and is named ${imgname[0]}");
}
else {
print("No Picture");
}
[/code]

Can anyone see what I'm doing wrong?  I tried doing this, but get a parse error:
[code]
if (!empty(${imgname[0]}) and file_exists("images/${imgname[0]}")) {
print("Picture Exists and is named ${imgname[0]}");
}
else {
print("No Picture");
}
[/code]


Thanks,
Erica
0
Comment
Question by:Job4Suprmn
3 Comments
 
LVL 9

Expert Comment

by:techtonik
ID: 11919690
First of all can you provide contents of your $_FILES by putting
echo "<pre"; print_r($_FILES);
in the beginning of the source?
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 50 total points
ID: 11919737
file_exists returns true when the argument is a file [u]or directory[/u]. If you want to check for files only you could use
if(is_file('images/'.$imgname[0])) which only returns true when it finds a actual file.
0
 

Author Comment

by:Job4Suprmn
ID: 11921650
Thanks Roonaan, that worked like a charm!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now