Solved

No file uploaded, but code says it exists

Posted on 2004-08-27
3
257 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 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

729 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