enctype and file upload - php

i made a form page to upload files
<form role="form" action="portfilio.php" method="post" enctype="multipart/from-data">
<textarea class="form-control textarea" name="desc"></textarea>
<input type="file" name= "img" class="custom-file-input" id="exampleInputFile">
when i want to test it if he is working or not by using this code
if (isset($_POST['desc'])) { echo '<pre>'; print_r($_FILES);die; }
should send somthing like
result
but not just empty array like :
Array ()
but why ? where the wrong ??

(1) Please fix the typo and use enctype="multipart/form-data"
(2) Please use $_POST and $_FILES to get the submitted data details, as follows:
print_r($_POST);
print_r($_FILES);
So , amend your code to
HTML
<form action="test1.php" method="post" enctype="multipart/form-data">
<textarea class="form-control textarea" name="desc"></textarea>
<input type="file" name= "img" class="custom-file-input" id="exampleInputFile">
<input type=submit>
and
test1.php
<?php
if (isset($_POST['desc'])) {
print_r($_POST);
echo "<br>";
print_r($_FILES);
//die;
}
?>
The result (if you type something in the textarea and pick a file, then submit) will be like (well tested):
Array ( [desc] => Test1 Test2 )
Array ( [img] => Array ( [name] => 2.jpg [type] => image/jpeg [tmp_name] => /tmp/phpO9vL4C [error] => 0 [size] => 22904 ) )

try running. print_r($_POST);
It will show all the post output also sometime editing a template creates some problem so you have to do it with care.

Related

How to upload multiple images from array to a local server folder from php

What I'm trying to do upload multiple images from a single click using POST method. Path of each image saving in Database as I want to but nothing getting stored on that local folder path. I am taking multiple images from multiple inputs, not from the single input.
Input File to upload single then click on ADD to add another from another file_input tag.. and so on.
Array ( [0] => Array ( [attachment] => 8.jpg ) [1] => Array ( [attachment] => 3-D80A.JPG ) )
Front
<form method="post" action="">
<input type="file" id="file" name="attachment[0][attachment]">
<input type="file" id="file" name="attachment[1][attachment]">
<input type="submit" name="submit_case" value="Save" class="btn btn-primary">
</form>
Back
foreach (array_keys($_FILES["attachment"]["name"]) as $fieldKeysymtoms) {
foreach ($_FILES["attachment"]["name"][$fieldKeysymtoms] as $key=>$value) {
print_r($_FILES["attachment"]["name"][$fieldKeysymtoms]);
if(move_uploaded_file($_FILES["attachment"]["tmp_name"][$fieldKeysymtoms], "app-assets/images/".$name)){
$uploads = "app-assets/images/".$name;
}
$json_symtoms_array+=array($fieldKeysymtoms => $value);
}
}

Problems with $_FILES in PHP

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?
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>
if(!empty($_FILES['file']['size'])):
$file = $_FILES['file']['tmp_name'];
//play with the file then
endif;
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
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
)
)

$_FILES['file'] does not work for a simple procedure

Unable to upload an image
The problem is that the image does not get set as `$_FILES['file'].
The code is very simple but still cannot get the obvious outcome.
Have marked the unexpected trouble with 3* i.e. '*'
I don't know what I am doing wrong.
Thanks in advance, your suggestions and comments are much appreciated.
Happy Coding!
<--- NOTE: HTML form below PHP --->
<?php
//CHECK: Submit
if(isset($_POST['submit']))
{
echo 'form submitted <br />';
//CHECK: File by POST
if(isset($_POST['file']))
{
echo $_POST['file'] . '<br />';
unset($_POST['file']);
}
//CHECK: File if 'isset' $_FILES
if(isset($_FILES['file']))
{
print_r($_FILES['file']);
}
else
{
echo '$_FILES not set!';
}
}
?>
<!-- HTML FORM -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="5M">
Image: <input type="file" name="file" size="50" id="file" />
<br /><br />
<input style="margin-left:15%;" type="submit" name="submit" value="Upload"/>
</form>
Please correct your form action you missed `echo :-
<?php echo $_SERVER['PHP_SELF']; ?>
^
As your comment, your are getting error 2 that means:-
UPLOAD_ERR_FORM_SIZE
Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was
specified in the HTML form.
Please refer this solution as well File upload ['ERROR']= 2
Following correction are needed.
1 - Add an echo in action="<?php $_SERVER['PHP_SELF']; ?>" as action="<?php echo $_SERVER['PHP_SELF']; ?>"
2 - Why your are finding file in $_POST, need a check to it
if(isset($_POST['file'])){
echo $_POST['file'] . '<br />';
unset($_POST['file']);
}
3 - You have commented your out put is from line print_r($_FILES['file']);
Array (
[name] => imagename.jpg
[type] => [tmp_name] =>
[error] => 2
[size] => 0
)
tmp_name not getting blank that means your file is not creating its temporary file to upload.
So, check temp upload file path in php.ini make sure it is working
different value in name attribute
Image: <input type="file" name="fileupload" size="50" id="file" />
<?php
if(isset($_POST["submit"]))
{
$name=$_FILES['fileupload']['name'];
$name=$_FILES['fileupload']['tmp-name'];
$name=$_FILES['fileupload']['size'];
}
?>

PHP File Uploading - No File Uploaded Error

I've created an image uploading form , using twitter bootstrap and the jansy extention, using the next code section:
<form action="fileName.php" method="post" enctype="multipart/form-data">
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="fileupload-preview thumbnail" style="width: 200px; height: 150px;"></div>
<div>
<span class="btn btn-file"><span class="fileupload-new">Select image</span><span class="fileupload-exists">Change</span><input type="file" name="pic" id="pic"/></span>
Remove
<button type="submit" class="btn">Upload</button>
</div>
</form>
on the fileName.php the following code is executed:
print_r($_FILES);
if($_FILES["pic"]["error"] > 0){ /*image uploading has failed*/
echo "Error occured ".$_FILES["pic"]["error"];
}
else {
echo "image was uploaded successfully";
}
and returns the next message :
Array ( [pic] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) ) Error occured 4
How come the file is not uploaded ? How could I solve this?
Thanks.
Error 4 means UPLOAD_ERR_NO_FILE, so no file has been posted. I'd say the problem is somewhere in your javascript code or the components you used. Is any JavaScript error thrown?
Try a simple
<form action="fileName.php" method="post" enctype="multipart/form-data">
<input type="file" name="pic">
</form>
to see if it works without any JavaScript plugins.

How can I debug problems with move_uploaded_file?

I have a form like
<form action="send.php" method="post" enctype="multipart/form-data">
<div>
<label for="subject">Subject</label>
<input type="text" name="subject" />
</div>
<div>
<label for="image">Image</label>
<input type="file" name="image" />
</div>
<input type="submit" value="Send" />
</form>
PHP like
echo '<pre>'; print_r($_FILES); echo '</pre>';
if (move_uploaded_file($_FILES['image']['tmp_name'], 'images/' . $_FILES['image']['name'])) {
echo 'ok';
} else {
echo 'error!';
};
I keep getting error the print_r looks like
Array
(
[image] => Array
(
[name] => Untitled-1.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpprWdjN
[error] => 0
[size] => 61768
)
)
Activate error reporting, then you should see the error thrown by move_uploaded_file telling you what's wrong.
Your $_FILES looks file, error=0 means the upload completed successfully. Most likely it's a permissions error. You can try doing something like:
if (!is_writeable('images/' . $_FILES['image']['name'])) {
die("Cannot write to destination file");
}
However, be aware that you're using a user-provided filename, so if someone uploads "pwn_my_server.php", your script will write it out to the images directory, and then they can simply visit yoursite.com/images/pwn_my_server.php and take control of your site.
In general it is NEVER a good idea to trust anything in the $_FILES array, or use it directly, since the entirety of its contents are under remote user control. The only thing created by the server is the error code and tmp_name. The rest is potentially malicious.
maybe the problem is 'image/' folder, you can set the absolute path here and make sure that path is writable, then have a try.
Use the code below:
1. create the directory named 'uploads'
2. save the file with .php extension
now run the code.
<?php
if (!empty($_FILES))
{
// PATH TO THE DIRECTORY WHERE FILES UPLOADS
$file_src = 'uploads/'.$_FILES['image']['name'];
// FUNCTION TO UPLOAD THE FILE
if(move_uploaded_file($_FILES['image']['tmp_name'], $file_src)):
// SHOW THE SUCCESS MESSAGE AFTER THE MOVE - NO VISIBLE CHANGE
echo 'Your file have been uploaded sucessfuly';
else:
// SHOW ERROR MESSAGE
echo 'Error';
endif;
}
?>
<form action="" method="post" enctype="multipart/form-data">
<div>
<label for="subject">Subject</label>
<input type="text" name="subject" />
</div>
<div>
<label for="image">Image</label>
<input type="file" name="image" />
</div>
<input type="submit" value="Send" name='submit' />
</form>
:)

Categories