Reputation: 436
I'm having some problems with the move_upload_file function. I always get "Upload not valid" message. Here the php and html code. Can you guys see some errors? The upload directory is writable as you can see from the picture below.
ls -l command output ls -l command output
Here the PHP
$tmp_dir = $_FILES['avatar']['tmp_name'];
$imgSize = $_FILES['avatar']['size'];
$upload_dir = '../../img/useravatar/';
$userpic = hash("md5", time() . $imgFile) . "." . $imgExt;
if ($imgSize < 5000000) {
if (!is_writable($upload_dir))
die("Permission denied when uploading file");
if (move_uploaded_file($tmp_dir, $upload_dir . $userpic)) {
//success
die("Success");
}else{
//fail
die("Upload not valid");
}
}else{
redirect("../mainPages/signup.php", "Too large");
}
Here the HTML
<form class="form-signup" enctype="multipart/form-data"action="../submitPages/newUser.php" method = "post">
<div class="form-group">
<label for="avatar">Choose avatar</label>
<input type="file" class="form-control-file" name="avatar" accept="image/*" id="avatar">
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign Up</button>
<form>
Here the var_dump($_FILES)
array (size=1)
'avatar' =>
array (size=5)
'name' => string 'IMG_20160725_134910.jpg' (length=23)
'type' => string '' (length=0)
'tmp_name' => string '' (length=0)
'error' => int 1
'size' => int 0
Here the ls -l commands
drwxrwxrwx 3 nicola nicola 4096 Jan 27 02:31 img
drwxrwxrwx 2 nicola nicola 4096 Jan 15 03:37 useravatar
EDIT: It works with .png img.
Upvotes: 1
Views: 82
Reputation: 5708
You need to add enctype="multipart/form-data"
as an attribute to your <form>
to send pictures correctly (you must add this when sending any <input type="file">
items).
So your <form>
should look like something like this:
<form enctype="multipart/form-data" method="POST" action="path/to/PHPfile.php">
<div class="form-group">
<label for="avatar">Choose avatar</label>
<input type="file" class="form-control-file" name="avatar" accept="image/*" id="avatar">
</div>
<input type="submit" value="submit" />
</form>
If after setting the proper encoding type it still doesn't work try checking if the folder you are specifying exists in that path (correct might be ./../img/useravatar/
instead of ../../img/useravatar/
, etc.).
if(!file_exists("../../img/useravatar/")) echo 'no such directory';
EDIT:
Now that you posted the var_dump($_FILES)
the error is evident. The image you are uploading exceeds the maximum size limit defined in php.ini.
PHP returns an appropriate error code along with the file array. The error code can be found in the error segment of the file array that is created during the file upload by PHP. In other words, the error might be found in
$_FILES['userfile']['error']
.UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the
upload_max_filesize
directive in php.ini.
more on error codes here: php.net/file-upload-errors.
To set the values properly in php.ini you need to set the following fields:
memory_limit = 32M // size of post_max_size or greater
// this is the size of the whole POST
post_max_size = 32M // should be greater than upload_max_filesize
// this is max size per uploaded file
upload_max_filesize = 24M
Alternatively, these settings can also be applied in .htaccess
:
# this should be added at the bottom of the .htaccess file
php_value memory_limit 32M
php_value post_max_size 32M
php_value upload_max_filesize 24M
Upvotes: 2