views:

75

answers:

2

I am trying to write a script which uploads a file via a html form. When I click submit nothing happens.

file: upload_form.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

<form action="do_upload.php" method="post" enctype="multipart/form-data"></form>
<p><strong>File to upload</strong></p>
<p><input name="img1" type="file" size="30" /></p>
<p><input name="submit" type="submit" value="Upolad File" /></p>

</body>
</html>

file: do_upload.php

<?php
if ($_FILES[img1] != "" {
    @copy($_FILES[img1] [tm_name], "/tmp" .$_FILES[img1][name])
    or die("couldnt copy the file");
} else {
    die("no file specified");
}
?>

<HTML>
<head>
<title>Successfull File Upload</title>
</head>
<body>

<h1>Success</h1>
<p>You sent: <? echo $_FILES[img1][name]; ?>, a <? echo $_FILES[img1][size]; ?>byte filw with a mime type of <? echo $_FILES[img1][type]; ?></p>

</body>
</HTML>
+5  A: 
<form action="do_upload.php" method="post" enctype="multipart/form-data"></form>
<p><strong>File to upload</strong></p>
<p><input name="img1" type="file" size="30" /></p>
<p><input name="submit" type="submit" value="Upolad File" /></p>

to

<form action="do_upload.php" method="post" enctype="multipart/form-data">
<p><strong>File to upload</strong></p>
<p><input name="img1" type="file" size="30" /></p>
<p><input name="submit" type="submit" value="Upolad File" /></p>
</form>

and

if ($_FILES[img1] != "" {

to

if (isset($_FILES['img1'])) {
Josh K
how silly of me, thanks!
Jacksta
Well, now my answer looks pretty dumb. :) That close tag was staring at me the whole time and I never noticed it.
awgy
Now I am getting a error on line 2 do_upload.php file :(
Jacksta
@Jacksta Exactly, there's the other error. Hint: There's no closing parenthesis for the `if (`.
deceze
well spotted, cheers
Jacksta
Though as mentioned in the other comments, use `isset()` to check. Updated my answer.
Josh K
where is the tmp directory located if tyhis script is hosted at this address. /domains/domain.com.au/public_html
Jacksta
It would be under (iirc) `$_FILES['photo']['tmp_name']`.
Josh K
And it sounds like you would very much benefit from [reading a tutorial on file uploads](http://www.tizag.com/phpT/fileupload.php).
Josh K
I dont this I was to clear in my last comment, I am going off a tutorial from some php book i am reading, in the example there tmp file is located at "/user/local/apache2/htdocs" I dont know where my tmp folder is located. e.g. what do I put here "" The script is locatedhere /domains/domain.com.au/public_html on an apache server
Jacksta
Try reading the tutorial I linked to instead of what ever you're using in the book.
Josh K
+3  A: 

If you're not receiving any errors at all, add the following lines to the top of your script to ensure that error reporting is set up properly:

error_reporting(E_ALL);
ini_set('display_errors', 'On');
awgy
If it's an error. He isn't getting **anything** because his `<form`> tags are out of wack.
Josh K