?
Solved

No file uploaded, but code says it exists

Posted on 2004-08-27
3
Medium Priority
?
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

777 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