Solved

No file uploaded, but code says it exists

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
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…

734 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