I have tried a number of things and all of them have crashed. My code is below. I am writing code for a form to upload three photos and this code works fine to upload three photos.

What I am stuck with is:

1) I would like the form to echo out a statement that says the "File already exists" and not overwrite the existing file

and

2) I would like the form to echo out the details of the three files and that they were successfully uploaded after this is executed.

Any help would be appreciated!
Thanks in advance.

<?php
if (isset($_FILES['upload']) === true) {
    $files = $_FILES['upload'];

    for($x = 0; $x < count($files['name']); $x++){
        $name       = $files['name'][$x];
        $tmp_name   = $files['tmp_name'][$x];

        move_uploaded_file($tmp_name, 'uploads/' . $name);

    }

}
?>

<form action="" method="post" enctype="multipart/form-data">
<table border="0" width="400" cellspacing="0" cellpadding="0" align="center">
<tr><td>Photo 1</td><td><input type="file" name="upload[]"></td></tr>
<tr><td>Photo 2</td><td><input type="file" name="upload[]"></td></tr>
<tr><td>Photo 3</td><td><input type="file" name="upload[]"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Upload Photos"></td></tr>
</table> 


</form>

Hi,

Can you try this ?

 for($x = 0; $x < count($files['name']); $x++){
    $name = $files['name'][$x];
    if (file_exists($name)) {
    echo 'File already exist';
    }
    else{
    $tmp_name = $files['tmp_name'][$x];
    move_uploaded_file($tmp_name, 'uploads/' . $name);
    }
    }

try it out first, I will help you out with the echo part of your question later.. My LAMPP is tied up with the JAVA testing..

Thanks veedeoo. I tried your suggestion like below...and it overwrote the file with no message.

<?php
if (isset($_FILES['upload']) === true) {
    $files = $_FILES['upload'];

    for($x = 0; $x < count($files['name']); $x++){
    $name = $files['name'][$x];
    if (file_exists($name)) {
    echo 'The file already exist';
    }
    else{
    $tmp_name = $files['tmp_name'][$x];
    move_uploaded_file($tmp_name, 'photos/' . $name);
    }
    }

}
?>

try changing this part .. sorry about that...my head just got lost its PHP focus..

 if (file_exists('photos/' .$name)) {
   echo 'The file already exist';
  }

Thanks veedeoo. It echos the 'The file already exist' three times, but I can live with that.

Any help on #2 of my OP would be greatly appreciated.

Another question has come to mind.

3) I want the photos to end up in a folder called /images/photos so based on the coding above I have used:

move_uploaded_file($tmp_name, 'photos/' . $name);

...and have uploaded this file in the images directory. In a perfect world, I would like to put it into a folder called admin/cms/upload and put the files into the images/photos directory. I cant seem to get it to work though....

just add something like this

move_uploaded_file($tmp_name, '/upload/images/photos/' . $name);
echo  '<img src="uploads/'.$name.'"/>';

on the file_exists declaration, just update the location..

The script above shows the image uploaded... you can change it to your #2 request.

I will look into this again tomorrow, maybe we can improve your script a little...

Thanks veedeoo....got it done!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.