0

I am trying to make a site where I can upload pictures.

<form action="edit.php" method="get" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="submit">
</form>

In the edit.php-file I have chosen to use the example from W3Schools (http://www.w3schools.com/php/php_file_upload.asp). When I get to the editor-page it won't upload because the file is a invalid type (filetype not found in the array).

After many different tries I putted this code at the top of the file:

if (!isset($_FILES['file'])) { die("Not found!"); }

When I loaded the editor page again I got the error-message I had put there myself. It seems like the file I am sending from the index.php-page won't be founded in edit.php.

Can anyone help me?

isimagan
  • 11
  • 2
  • 2
    Opinion: You should not use or link to [w3schools](http://www.w3fools.com). It's not a reliable source of information and we don't want to encourage its use. I recommend using the [Mozilla Developer Network](https://developer.mozilla.org/en-US/) instead. – John Conde Jun 08 '14 at 16:21
  • 3
    Your form's method should be `method='post'` not `method='get'` – Michael Berkowski Jun 08 '14 at 16:21
  • To familiarize yourself with the structure of `$_FILES` once you have gotten it to populate properly, do `print_r($_FILES);` in the receiving code `edit.php`. – Michael Berkowski Jun 08 '14 at 16:24
  • Thank you Michael Berkowski. It worked after I changed the form-method! – isimagan Jun 08 '14 at 16:33

4 Answers4

0

It should be like this and then in your edit.php file you can try to print the $_FILES array.

//edit.php
print '<pre>';
print_r($_FILEs);


// index.php
<html>
<body>

<form action="edit.php" method="post" enctype="multipart/form-data">
  <label for="file">Filename:</label>
  <input type="file" name="file" id="file"><br>
  <input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0
if(!empty($_FILES['file']['size'])):

$file = $_FILES['file']['tmp_name'];
//play with the file then
endif;
kay
  • 337
  • 3
  • 7
0

Change this line

<form action="edit.php" method="get" enctype="multipart/form-data">

to

<form action="edit.php" method="post" enctype="multipart/form-data">

See here for discussion on this topic. File uploading using GET Method

Community
  • 1
  • 1
crafter
  • 6,246
  • 1
  • 34
  • 46
0

Use post method. index.php code :

<form enctype="multipart/form-data" action="edit.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

edit.php (Just printed Files array and output is as follows) Code: "; print_r($_FILES); print ""; ?>

Output : Array ( [uploadedfile] => Array ( [name] => ipmsgclip_r_1398851755_0.png [type] => image/png [tmp_name] => /tmp/phpktZwLl [error] => 0 [size] => 155 ) )

If file uploading has any error then it will come in this array as [error]. For instance : I your uploaded file's size is exceeds the mentioned file size (1000000) Then output Array will be : Array ( [uploadedfile] => Array ( [name] => d.png [type] => [tmp_name] => [error] => 2 [size] => 0 ) )

Mohini
  • 407
  • 2
  • 4
  • 11