Here is a problem.
I have an HTML form with several fields in it.
One of the fields - 'Upload file'.
When I upload a file, everything works properly. But when I choose to submit the form without a file, it gives me the error message: "There was an error uploading the file, please try again". Looks to me that the script thinks that uploading a file is mandatory.
How do I change it?

Here is my PHP:

//File upload

// Where the file is going to be placed 
$target_path = "uploads/";

// Add the original filename to our target path.  
//Result is "uploads/filename.extension" 
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}	

//End of file upload

Thank you!

Member Avatar for diafol

just do an if(isset($_FILES....)) to check for an uploaded file

Could you add your HTML code, as well as your php.ini file?

just do an if(isset($_FILES....)) to check for an uploaded file

Thank you!
Actually, I fixed it with this:

if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) {

Now, though, I got another problem.
I want the file to be deleted from the directory after it was sent as an attachment with an email. When there is a file uploaded, it all works fine. But if there isn't, I get this error message:

Warning: unlink(uploads/) [function.unlink]: Is a directory in /mydomain.com/Hawaii/html/contact_email.php on line 191

How do I fix it?

Could you add your HTML code, as well as your php.ini file?

Actually, I was able to fix it with this:

if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) {

Now, though, I got another problem.
I want the file to be deleted from the directory after it was sent as an attachment with an email. When there is a file uploaded, it all works fine. But if there isn't, I get this error message:

Warning: unlink(uploads/) [function.unlink]: Is a directory in /mydomain.com/Hawaii/html/contact_email.php on line 191

How do I fix it?

Member Avatar for diafol

check to see that the file exists first;

if(file_exists(..filename..)){
  ...do your delete stuff...

}

check to see that the file exists first;

if(file_exists(..filename..)){
  ...do your delete stuff...

}

What should I use for the filename? This:
$_FILES
?

check to see that the file exists first;

if(file_exists(..filename..)){
  ...do your delete stuff...

}

Okay, now it looks like this:

if(file_exists($_FILES['uploadedfile']['name'])){
unlink($_FILES['uploadedfile']['name']);
}

and doesn't give an error message, but the file doesn't get deleted either.

Member Avatar for diafol

You probably need to specify the actual path from webroot as opposed to the $_FILE variable:

$pathfile = $_SERVER['DOCUMENT_ROOT'] . "/images/" . $_FILES['uploadedfile']['name'];
if(file_exists($pathfile)){
unlink($pathfile);
}

I used '/images/' as the folder you uploaded your file to. Change this to the actual folder you used.

You probably need to specify the actual path from webroot as opposed to the $_FILE variable:

$pathfile = $_SERVER['DOCUMENT_ROOT'] . "/images/" . $_FILES['uploadedfile']['name'];
if(file_exists($pathfile)){
unlink($pathfile);
}

I used '/images/' as the folder you uploaded your file to. Change this to the actual folder you used.

No, the same result, alas.

Member Avatar for diafol

Can't see why it wouldn't work.

Get the full path of the uploaded file - save it and paste it into the delete file snippet to see if it works. If not, come back.

if(file_exists($yourfullpathtofile)){
echo "file found";
unlink($yourfullpathtofile);
}else{
echo "no file found";
}

Can't see why it wouldn't work.

Get the full path of the uploaded file - save it and paste it into the delete file snippet to see if it works. If not, come back.

if(file_exists($yourfullpathtofile)){
echo "file found";
unlink($yourfullpathtofile);
}else{
echo "no file found";
}

Okay, I'm back. :)
It's not finding the file. "no file found" is what is displayed, and the file, although in the directory, doesn't get deleted.
Here is my code:

$yourfullpathtofile = "http://eximi.dreamhosters.com/Hawaii/html/uploads/CablevisionBill.doc";

if(file_exists($yourfullpathtofile)){
echo "file found";
unlink($yourfullpathtofile);
}else{
echo "no file found";
}
Member Avatar for diafol

No, I'm sorry I didn't explain myself properly. You need to use the webroot path:

If you do echo $_SERVER, you'll see it, it might be something like this:

/home/fslinux113/m/eximi.dreamhosters.com/user/htdocs

SO your file would be something like:

/home/fslinux113/m/eximi.dreamhosters.com/user/htdocs/Hawaii/html/uploads/CablevisionBill.doc

NOTE: This path IS NOT correct for you - just an example.

commented: Ardav is amazing - very knowledgeable, friendly, willing and able to share his knowledge in the most clrea and scrupulous manner. +1

Thank you so, so, so much!
The problem was - of course, I gave an incorrect path to the script.
Now it works flawlessly!

Oh no...
It's come back.
When I do this:

$pathfile = "/home/vitusya/eximi.dreamhosters.com/Hawaii/html/uploads/CablevisionBill.doc";

if(file_exists($pathfile)){
echo "file found";
unlink($pathfile);
}else{
echo "no file found";
}

all works perfectly well.
When I do this:

$pathfile = "/home/vitusya/eximi.dreamhosters.com/Hawaii/html/uploads/" . $_FILES['uploadedfile']['name'];

if(file_exists($pathfile)){
echo "file found";
unlink($pathfile);
}else{
echo "no file found";
}

in case I do upload a file, it works fine - uploads the file, sends the emails with the attachment, deletes the file.
But if I submit the form without a file, it gives me this:

file found
Warning: unlink(/home/vitusya/eximi.dreamhosters.com/Hawaii/html/uploads/) [function.unlink]: Is a directory in /home/vitusya/eximi.dreamhosters.com/Hawaii/html/contact_email.php on line 206

What is it it's finding in the empty folder?:-O

Member Avatar for diafol

Well obviously. Sorry.

You only do a $_FILES[...] if you've uploaded a file. If there is no file upload, there can be no $_FILES[...] variable, so when you use it, you get an error.

Place an isset around the whole shebang:

if(isset($_FILES['uploadfile'])){
 $pathfile = "/home/vitusya/eximi.dreamhosters.com/Hawaii/html/uploads/" . $_FILES['uploadedfile']['name'];
 if(file_exists($pathfile)){
  echo "file found";
  unlink($pathfile);
 }else{
  echo "no file found";
 }
}

I remember reading somewhere that it's more correct to search for the missing file:

i.e. if(!file_exists(...)) etc.

But that's up to you.

Well obviously. Sorry.

You only do a $_FILES[...] if you've uploaded a file. If there is no file upload, there can be no $_FILES[...] variable, so when you use it, you get an error.

Place an isset around the whole shebang:

if(isset($_FILES['uploadfile'])){
 $pathfile = "/home/vitusya/eximi.dreamhosters.com/Hawaii/html/uploads/" . $_FILES['uploadedfile']['name'];
 if(file_exists($pathfile)){
  echo "file found";
  unlink($pathfile);
 }else{
  echo "no file found";
 }
}

I remember reading somewhere that it's more correct to search for the missing file:

i.e. if(!file_exists(...)) etc.

But that's up to you.

Still same error message...
I tried both your version above and this one:

if(isset($_FILES['uploadedfile'])){
$pathfile = "/home/vitusya/eximi.dreamhosters.com/Hawaii/html/uploads/" . $_FILES['uploadedfile']['name'];
echo "file found";
unlink($pathfile);
}else{
echo "no file found";
}

Same result... I already feel bad bothering you.

Member Avatar for diafol

Don't worry, if I wasn't prepared to help, I wouldn't have replied.

I can suggest a directory scan for files - see the php manual:

if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    while (false !== ($file = readdir($handle))) {
        echo "$file\n";
    }

   closedir($handle);
}

Thi should check to see which files are present in your directory. Are you sure that your files are being stored in the expected directory?

Member Avatar for diafol

Hold on, you've done the move thing right?

move_uploaded_file ( string $filename, string $destination )

If you don't specify your destination, the file won't be there. See handling file uploads in the php manual. I'm assuming that you've just used 'is_uploaded'. I think I'm right in thinking that you need move_uploaded to actually place it in the directory in the first place.

Hold on, you've done the move thing right?

move_uploaded_file ( string $filename, string $destination )

If you don't specify your destination, the file won't be there. See handling file uploads in the php manual. I'm assuming that you've just used 'is_uploaded'. I think I'm right in thinking that you need move_uploaded to actually place it in the directory in the first place.

Yes, Alan, it's done right.
You'll be surprised at what made it all work!
Here:
@unlink.
I don't know why. To me it's a mystery and magic, although my brother-in-law (also a programmer) says there are no such things in programming as mystery and magic. :)
This is what my code looks like now:

if(isset($_FILES['uploadedfile'])){
$pathfile = "/home/vitusya/eximi.dreamhosters.com/Hawaii/html/uploads/" . $_FILES['uploadedfile']['name'];
@unlink($pathfile);

Thank you so much for all your help!
Vita

Member Avatar for diafol

The @ just stops an error from being shown, if there is one. OK, if you're happy with that, fantastic. Result!

The @ just stops an error from being shown, if there is one. OK, if you're happy with that, fantastic. Result!

Yes, it deletes the file if there is one uploaded, and doesn't print any messages in the browsers if there is nothing there to delete.
I'll try and dig it further, but for now my task is completed. :)

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.