0

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!

Edited by levsha: n/a

3
Contributors
21
Replies
22
Views
7 Years
Discussion Span
Last Post by levsha
Featured Replies
  • 1
    diafol 3,720   7 Years Ago

    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. [CODE]if(file_exists($yourfullpathtofile)){ echo "file found"; unlink($yourfullpathtofile); }else{ echo "no file found"; }[/CODE] Read More

  • 1
    diafol 3,720   7 Years Ago

    No, I'm sorry I didn't explain myself properly. You need to use the webroot path: If you do [B]echo $_SERVER['DOCUMENT_ROOT'][/B], you'll see it, it might be something like this: [B]/home/fslinux113/m/eximi.dreamhosters.com/user/htdocs[/B] SO your file would be something like: [B]/home/fslinux113/m/eximi.dreamhosters.com/user/htdocs/Hawaii/html/uploads/CablevisionBill.doc[/B] [COLOR="Red"]NOTE: This path IS NOT correct for you - just an example.[/COLOR] Read More

0

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

0

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?

Edited by levsha: n/a

0

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?

0

check to see that the file exists first;

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

}
0

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
?

0

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.

0

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.

0

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.

1

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";
}

Edited by diafol: n/a

0

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";
}

Edited by levsha: n/a

1

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.

Edited by diafol: n/a

Votes + Comments
Ardav is amazing - very knowledgeable, friendly, willing and able to share his knowledge in the most clrea and scrupulous manner.
0

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

0

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

0

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.

Edited by diafol: n/a

0

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.

0

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?

0

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.

0

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

0

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

0

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. :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.