PHP move_uploaded_file() error?
Check that the web server has permissions to write to the "images/" directory
PHP move_uploaded_file() FAILS and i don't know why
Are you sure that the target directory has write permissions for world
?ie,the third number in permission representation?
The files uploaded by php are owned by and comes under the group www-data
You can change the ownership by
[sudo] chown -R www-data folder // change owner
[sudo] chown -R www-data:www-data folder // change group and owner
PHP move_uploaded_file fails with no errors
Just in case others run into this problem, I want to share what I discovered. I was able to conclude this was an issue with Linux CentOS, and not a PHP issue.
Typically, open_basedir
will be set to /var/www
in /etc/php.ini
. In this scenario, upload_tmp_dir
must be somewhere below /var/www
. I set upload_tmp_dir
to /var/www/html/images/tmp
.
upload_tmp_dir = /var/www/html/images/tmp
I then set the temporary uploads directory permission is 755.
[john.doe@server1 ~]# chmod 755 /var/www/html/uploads/tmp
I then used the following PHP to determine what user PHP uses to upload the file. In my scenario, the user was apache.
<?php
echo exec('whoami');
?>
Once I had determined the user was apache, I set the owner of the temporary uploads directory to apache.
[john.doe@server1 ~]# chown apache:root /var/www/html/uploads/tmp
Lastly, I had to set the SELinux context of the temporary and permanent uploads directory to httpd_sys_rw_content_t
.
[john.doe@server1 ~]# chcon --type httpd_sys_rw_content_t /var/www/html/uploads
[john.doe@server1 ~]# chcon --type httpd_sys_rw_content_t /var/www/html/uploads/tmp
After doing these things, I was then able to upload files using PHP.
PHP move_uploaded_file results in error #1
OK, i was dumb. I changed the wrong php.ini file. For anyone who experiences similar issues you can check the location of the php.ini file and if it's loaded by PHP with:
$inipath = php_ini_loaded_file();
if ($inipath) {
echo 'Loaded php.ini: ' . $inipath;
} else {
echo 'A php.ini file is not loaded';
}
Hope this helps.
How can I debug problems with move_uploaded_file?
Activate error reporting, then you should see the error thrown by move_uploaded_file
telling you what's wrong.
php file upload error warning move_uploaded_file failed to open stream permission denied in
I have tried the same code and it works for me. I have made some changes for you.
<form method="POST" enctype="multipart/form-data">
<label for="">Upload Your Cv</label><input type="file" name="file">
<input type="submit" name="upload" value="upload">
</form>
After that you don't need to redirect the page; instead you can use this, below the </form>
if(isset($_REQUEST["upload"]))
{
if (isset($_FILES['file'])) {
$file = $_FILES['file'];
// print_r($file); just checking File properties
// File Properties
$file_name = $file['name'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
// Working With File Extension
$file_ext = explode('.', $file_name);
$file_fname = explode('.', $file_name);
$file_fname = strtolower(current($file_fname));
$file_ext = strtolower(end($file_ext));
$allowed = array('txt','pdf','doc','ods');
if (in_array($file_ext,$allowed)) {
//print_r($_FILES);
if ($file_error === 0) {
if ($file_size <= 5000000) {
$file_name_new = $file_fname . uniqid('',true) . '.' . $file_ext;
$file_name_new = uniqid('',true) . '.' . $file_ext;
$file_destination = 'upload/' . $file_name_new;
// echo $file_destination;
if (move_uploaded_file($file_tmp, $file_destination)) {
echo "Cv uploaded";
}
else
{
echo "some error in uploading file".mysql_error();
}
//
}
else
{
echo "size must bne less then 5MB";
}
}
}
else
{
echo "invalid file";
}
}
}
Note that the upload folder must be in the same directory as where you store the file.
PHP move_uploaded_file error
The destination of move_uploaded_file should be a filename:
$source = $_FILES['fpupload']['tmp_name'];
$upload_dir = "invform/upload/";
$dest = $upload_dir.$source; // gives: invform/upload/phpFB1.tmp
Check move_uploaded_file on the manual.
Related Topics
How to Send Email from Localhost Wamp Server to Send Email Gmail Hotmail or So Forth
Exception: Serialization of 'Closure' Is Not Allowed
Posting Multidimensional Array with PHP and Curl
MySQL - Count Total Number of Rows in PHP
How to Add Anchor Tag to a Url from Text Input
JavaScript Equivalent of PHP's List()
Check Whether $_Post-Value Is Empty
Naming Convention Issues When Using Codeigniter in Windows and Linux
Why We Need Curlopt_Ssl_Verifypeer in Windows
How to Check If There Is a There Is a Wget Instance Running
PHP Failed to Open Stream: Is a Directory
Centos 7/Apache/PHP - Mkdir(): Permission Denied
Execute Commands on Remote MAChine via PHP
How to Chain Method on a Newly Created Object
More Concise Way to Check to See If an Array Contains Only Numbers (Integers)
Woocommerce - Hide Other Shipping Methods When Free Shipping Is Available