I have PHP code like this:
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
$error = array();
if (isset($_POST['submit'])) {
$dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'upload';
if (!empty($_FILES)) {
$tmp = $_FILES['file']['tmp_name'];
$file = $dir . DIRECTORY_SEPARATOR . $_FILES['file']['name'];
if (is_uploaded_file($tmp)) {
if (!chown($dir, 'nobody')) {
$error[] = 'Owner tidak bisa di replace!';
}
if (!chmod($dir, intval(755, 8))) {
$error[] = 'Direktori "' . $dir . '" tidak bisa diberi akses!';
}
if (!move_uploaded_file($tmp, $file)) {
$error[] = 'Gagal memindahkan berkas "' . $file . '"';
}
if (file_exists($file)) {
$error[] = 'Berhasil di unggah! ' . $file;
}
} else {
$error[] = 'Berkas tidak bisa di unggah.';
}
}
}
?>
When running this code the following messages appear:
Warning: chown(): Operation not permitted in /var/www/html/jdih/upload.php on line 23
Warning: chmod(): Operation not permitted in /var/www/html/jdih/upload.php on line 24
Warning: move_uploaded_file(/var/www/html/jdih/upload/06-naiveBayes-example.pdf): failed to open stream: Permission denied in /var/www/html/jdih/upload.php on line 25
Warning: move_uploaded_file(): Unable to move '/tmp/phpMHL5CQ' to '/var/www/html/jdih/upload/06-naiveBayes-example.pdf' in /var/www/html/jdih/upload.php on line 25
What's going on here?
You should verify file permissions and file owner.
To change file permissions :
chmod -R 775 /var/www/html/jdih
And set the owner to the user who run your apache server.
chown www-data:www-data /var/www/html/jdih
Note : the owner depends on your distribution, on Ubuntu it's www-data, it could be httpd or http or something else.
Go to terminal, and type:
chmod -R 775 /your/upload/folder/path
then you set the permission to write in this dir
Related
I'm trying to extract zip files from a folder into a subfolder. The error I'm recieving is:
Warning: ZipArchive::extractTo(XXXX): failed to open stream: Permission denied in /var/www/html/update_alerts2.php on line 97
extracted
Code :
public function extractFiles($inputDir,$outputDir) {
$files_to_extract = $this->getFiles($inputDir);
$zip = new ZipArchive();
foreach ($files_to_extract as $file) {
echo $file;
$res = $zip->open($inputDir . $file);
if ($res) {
$zip->extractTo($outputDir);
$zip->deleteName($inputDir . $file);
$zip->close();
} else {
echo 'failed, code:' . $res;
}
}
}
I've tried setting permissions to 777 but I still get the error. What are the possible ways I can fix this issue (I'm using centos7 and apache) ?
I just wanna ask if its possible to make directory and store the uploaded file in it to a shared folder from another pc using mkdir and move_uploaded_file. I tried doing it locally it works fine but when i try this code
if($_REQUEST['submit']){
$app_name = $_POST['name'];
$desc = $_POST['description'];
$file_url = "192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"];
$photo_loc = "192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"];
$select = $_POST['select'];
$date = date('m/d/y h:i:s A');
mkdir("192.168.3.34/testupload/files/$app_name/");
mkdir("192.168.3.34/testupload/images/$app_name/");
/* some codes here that is working */
if($file != "" ){
if (file_exists("..192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"]) && file_exists("..192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"])) {
die("File Exists");
}
move_uploaded_file($_FILES["file"]["tmp_name"],
"192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"]);
if($photo_url != "") {
move_uploaded_file($_FILES["photo"]["tmp_name"],
"192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"]);
}
$name = escape($_REQUEST['name']);
$description = escape($_REQUEST['description']);
//$query = "INSERT into files (name, description,file_name,photo_url,date_uploaded) values ('{$name}', '{$description}','{$file}', '{$photo_url}', now() )";
//mysql_query($query) or die("Error in Query" . mysql_error());
//commit();
//redirect("manage.php");
$file_loc = "192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"];
$photo_loc = "192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"];
}
}else{
set_error("File not Found");
//redirect('manage.php');
}
i tried to change the "/" in "\" and still getting this error
Warning: mkdir() [function.mkdir]: No such file or directory in
C:\xampp\htdocs\atts_update\upload.php on line 12
Warning: mkdir() [function.mkdir]: No such file or directory in
C:\xampp\htdocs\atts_update\upload.php on line 13
Warning:
move_uploaded_file(192.168.3.34/testupload/files/1111111111111/sss.txt)
[function.move-uploaded-file]: failed to open stream: No such file or
directory in C:\xampp\htdocs\atts_update\upload.php on line 71
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to
move 'C:\xampp\tmp\php610B.tmp' to
'192.168.3.34/testupload/files/1111111111111/sss.txt' in
C:\xampp\htdocs\atts_update\upload.php on line 71
Warning:
move_uploaded_file(192.168.3.34/testupload/images/1111111111111/Picture-in-a-picture4aaa.jpg) [function.move-uploaded-file]: failed to open stream: No such file or
directory in C:\xampp\htdocs\atts_update\upload.php on line 76
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to
move 'C:\xampp\tmp\php611C.tmp' to
'192.168.3.34/testupload/images/1111111111111/Picture-in-a-picture4aaa.jpg' in C:\xampp\htdocs\atts_update\upload.php on line 76
The error is Warning: move_uploaded_file(uploads/cv/23456543555555CURRICULUM VITAE.doc): failed to open stream: No such file or directory in C:\wamp\www\job-application\include\application.class.php on line 121
Warning: move_uploaded_file(): Unable to move 'C:\wamp\tmp\php7EA1.tmp' to 'uploads/cv/23456543555555CURRICULUM VITAE.doc' in C:\wamp\www\job-application\include\application.class.php on line 121
The code follows below;
$allowedExtsDoc = array("docx", "pdf", "doc");
$temp = explode(".", $_FILES["file"]["name"]);
$extensionDoc = end($temp);
$uploaddircv = 'uploads/cv/';
$uploadfilecv = $uploaddircv . basename($phone.$_FILES['file']['name']);
if (in_array($extensionDoc, $allowedExtsDoc))
{
if ($_FILES["file"]["error"] > 0)
{
$this->error .= $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists($uploaddircv . $_FILES["file"]["name"]))
{
$this->error .= $_FILES["file"]["name"] . " already exists. <br/>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],$uploadfilecv);
$fileloc = $this->sitePath.$uploadfilecv;
}
}
}
else
{
$this->error .= "Invalid file <br/>";
}
Make sure the directories: C:\wamp\tmp\ and uploads/cv/ exists and are writable by Apache.
After that, try to use the complete path to uploads folder.
$uploaddircv = realpath(dirname(__FILE__) . '/../uploads/cv/') . '/';
$ref_id = mysql_insert_id();
$locatie = 'iso_photo/afwijkingen';
if($_FILES['uploaded']['type'] != 'application/octet-stream') // Geen php files
{
$folder = $locatie.basename($_FILES['uploaded'][$ref_id.'.jpg']) ;
move_uploaded_file($_FILES['uploaded']['tmp_name'], $folder);
}
Gives:
Warning: move_uploaded_file(iso_photo/afwijkingen) [function.move-uploaded-file]: failed to open stream: Is a directory
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php6LQCqY' to 'iso_photo/afwijkingen'
What might be wrong?
edited 4th line, should work now
$locatie = 'iso_photo/afwijkingen';
if($_FILES['uploaded']['type'] != 'application/octet-stream') // Geen php files
{
$folder = $locatie . "/" . basename($_FILES['uploaded'][$ref_id]) ;
move_uploaded_file($_FILES['uploaded']['tmp_name'], $folder);
}
but can't properly say, as i don't know content of $ref_id variable
I'm trying to make a php script to connect to an afp server and get a directory listing (with each file size). The server is local in our office, but I'm unable to just make a script on the afp server side. On my machine, I use something like this:
$filesInDir = array();
$filesInMySQL = array();
if (is_dir($uploadDir)) {
$dh = opendir($uploadDir);
if ($dh) {
$file = readdir($dh);
while ($file != false) {
$path = $uploadDir . "/" . $file;
$type = filetype($path);
if ($type == "file" && $file != ".DS_Store" && $file != "index.php") {
$filesInDir[] = $file;
}
$file = readdir($dh);
}
closedir($dh);
} else {
echo "Can't open dir " . $uploadDir;
}
} else {
echo $uploadDir . " is not a folder";
}
But I can't connect to the afp server. I've looked into fopen it doesn't allow afp, and I don't think it'd allow directory listing:
opendir("afp://ServerName/path/to/dir/");
Warning: opendir() [function.opendir]: Unable to find the wrapper "afp" - did you forget to enable it when you configured PHP? in...
Warning: opendir(afp://ServerName/path/to/dir/) [function.opendir]: failed to open dir: No such file or directory in...`
I'm not looking to see if a file exists, but to get the entire directory listing. Eventually I'll also have to remotely copy files into an output directory.
eg.
mkdir afp://ServerName/output/output001/
cp afp://ServerName/path/to/dir/neededfile.txt afp://ServerName/output/output001/
Maybe use http://sourceforge.net/projects/afpfs-ng/ to mount it...
I'm developing on an Mac Mini, so I realised I could just mount the afp share, and use readdir. I had to mount the drive using the following:
sudo -u _www mkdir /Volumes/idisk
sudo -u _www mount_afp -i afp://<IP>/sharename/ /Volumes/idisk/
Further details here