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) ?
Related
Trying to create a new dir using:
$date = date('MDY');
$fileName = $date . '-organizations.xlsx';
$directory = "/guard/API/office/Storage/Custom Reports/";
if (!file_exists($directory) && !is_dir($directory)) {
mkdir($directory, 0777, true);
}
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save($directory . $fileName);
return $fileName;
But the error is:
Warning: mkdir(): Permission denied in \somedir\...
Could it be an issue of permissions on my local dev machine? I will NOT be able to set up permissions on the server.
Thanks!
Because you tried to make directory from root path. Try two way to fix error.
1.Change directory path from currently to right:
$directory = __DIR__."/guard/API/office/Storage/Custom Reports/";
Try this as root user:
exec ('sudo mkdir -m 777 '.$directory);
I've installed apache on a debian server and added php. But if i try to create a .zip file it doesn't work. There's no error message but the .zip file doesn't get created.
Code:
<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
$zip->addFile('Banner1.png', 'Banner1.png');
$zip->close();
?>
Most likely, the user that php is running as (on debian, www-data by default) doesn't have writing permissions to the directory. Check the result of the various functions like this:
<?php
$zip = new ZipArchive();
$res = $zip->open('test.zip', ZipArchive::CREATE);
if (!$res){
echo 'Error while creating zip file: ' . $zip->getStatusString();
exit();
}
if (! $zip->addFile('Banner1.png', 'Banner1.png')) {
echo 'Error while adding Banner1: ' . $zip->getStatusString();
exit();
}
if (! $zip->close()) {
echo 'Error while closing: ' . $zip->getStatusString();
exit();
}
On a debian with default configuraiton, you can configure permissions with chown and chmod. For example, to allow the webserver to write to that directory, try
sudo chown www-data /path/to/directory/with/php/file
sudo chmod u+rwx /path/to/directory/with/php/file
Sûre your $res is good ?
if ($res === TRUE) {
echo 'ok';
$zip->addFile('Banner1.png', 'Banner1.png');
$zip->close();
} else {
echo 'failed, code:' . $res;
}
$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
How is one supposed to handle files that aren't in the current directory when using ftp_put? This piece of code is trying to upload a file that I know exists, but it always gives the following error:
"Warning: ftp_put() [function.ftp-put]: Requested action not taken, file not found or no access. in /path/to/files/domains/mydomain.com/html/scriptfile.php on line 1337"
Here's the snip:
$file_name = $this->GetFileName();
if ($file_name)
{
$resource = ftp_connect('ftp.remoteftpserver.com');
if ($resource && ftp_login($resource, $username, $pass))
{
ftp_pasv($resource, true);
//UPLOAD_DIRECTORY == '/IN' (it really exists, I'm sure)
//ORDER_DIRECTORY == /home/domains/mydomain.com/orders (came from $_SERVER['DOCUMENT_ROOT']
ftp_put($resource, UPLOAD_DIRECTORY . '/' . $file_name, ORDER_DIRECTORY . '/' . $file_name, FTP_ASCII);
ftp_close($resource);
}
else
{
echo "FTP Connection Failed!";
}
}
Check the permissions of the remote file. Make sure $username has write access to the file. Make sure you have execute access on the parent directory.