Solved

No file uploaded, but code says it exists

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

12 Experts available now in Live!

Get 1:1 Help Now