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.
Related
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.
I am using Heroku and their S3 addon - "Bucketeer".
I have this working and I can upload images no problem. The thing is I don't care about uploading images. My app deals with uploading MP3s (nothing sinister - it's a web app for local bands).
I can't upload MP3s. It doesn't error out or anything, it's just that files don't end up in S3 - whereas images uploaded exactly the same way, do.
Editing to add - It's not a file type issue as I tested by uploading a very small mp3 file and this worked great.
Code below:
<form class="form" action="../model/process-track-upload.php" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-xs-12 col-sm-4">
<div class="dashboard-widget">
<h3>Add a song to your collection</h3>
<input class="input" type="text" name="trackname" placeholder="Track Name" required/>
<input class="input-margin" name="userfile" type="file">
<input type="hidden" name="userID" value="<?php echo $userID ?>" />
<button class="btn btn-large btn-block " type="submit" name="btn-upload">upload</button>
</div>
</div>
</form>
And the backend code:
require('../vendor/autoload.php');
$s3 = Aws\S3\S3Client::factory();
$bucket = getenv('S3_BUCKET')?: die('No "S3_BUCKET" config var in found in env!');
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['userfile']) && $_FILES['userfile']['error'] == UPLOAD_ERR_OK && is_uploaded_file($_FILES['userfile']['tmp_name'])) {
try {
$upload = $s3->upload($bucket, $_FILES['userfile']['name'], fopen($_FILES['userfile']['tmp_name'], 'rb'), 'public-read');
?>
<p>Upload successful :)</p>
<?php } catch(Exception $e) { ?>
<p>Upload error :(</p>
<?php } }
The answer to this question lay in default php settings.
HOWEVER as I was using Heroku, I didn't have access to the php.ini file.
So the answer was to create a .users.ini file which is like a user created override of the php.ini.
In that file I just set the following params:
upload_max_filesize = 15M
post_max_size = 15M
max_input_time = 0
LimitRequestBody 0
After this I was good to go!
There are multiple questions[duplicates] about this same error but I can't find a solution in any of them. I`m trying to save an image in my local directory but 'Undefined index' keeps showing up. I´m a noob in php and stack overflow. I would really appreciate any of your help.
HTML:
<form method="post" action="process.php" enctype="multipart/form-data">
<fieldset>
<div class="col-xs-12 col-sm-push-5 image-div form-control">
<div class="input-margin">
<input type="file" name="imagenIngreso" class="filestyle" onchange="readURL(this);" data-buttonText="Subir Imagen" data-buttonName="btn-primary" data-size="nr" data-buttonBefore="false">
</div>
<div>
<img class="image-preview" id="blah" src=""/>
</div>
</div>
<div class="col-xs-12 col-sm-12 submit-button">
<button type="submit" class="btn btn-primary btn-block" name="submit" value="ingreso">Ingresar</button>
</div>
</fieldset>
</form>
PHP:
// Previous Validations
else if($_POST['submit'] == 'ingreso') {
$fileName = $_FILES['imagenIngreso']['name']; // Line throwing error
$uploadDirectory = "images/uploadedImages/Ingresos/";
if($uploadedFileName!='') {
$targetPath=time().$uploadedFileName;
if(move_uploaded_file($_FILES['imagenIngreso']['tmp_name'], $uploadDirectory.$targetPath)) {
$queryIngresos = "INSERT INTO ingresos (fechaIngreso, responsable, proyecto, items, imagePath) VALUES (STR_TO_DATE('$fechaEgreso','%d/%m/%Y'), '$responsable', '$proyectoIngreso', '$itemsArray', '$targetPath')";
if(!mysqli_query($con, $queryEgresos)) {
echo "Error while uploading file";
die('Error :'.mysqli_error($con));
} else {
echo 'Upload done';
exit();
}
}
}
You can also find what error are you getting using the Error Codes available.
Put this and make a try.
<?php
if($_FILES['userfile']['error']) {
// handle the error
} else {
// process
}
?>
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
)
)
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>
:)