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;
}
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) ?
How can I move a file from one directory into another directory?
I have try this, but it doesn't work:
$success = #rename('ssh2.sftp://' . $sftp . $path_from, 'ssh2.sftp://' . $sftp . $path_to);
if ($success) {
echo "moving success!";
}else {
echo "moving failed!";
}
I'm using PHP SSH2 functions.
Can someone help me?
Use ssh2_sftp_rename:
ssh2_sftp_rename($sftp, $path_from, $path_to);
Assuming both variables contain full paths to files, e.g. like:
$path_from = "/source/directory/myfile.txt";
$path_to = "/destination/directory/myfile.txt";
The PHP version is 5.5.12 ( under WAMP 2.5 )
I want to create a directory recursively , it is on my development computer Windows7 at the moment but the production system is Linux :
define('RP_MAIN', $_SERVER['DOCUMENT_ROOT'] . 'impots/');
$dir = RP_MAIN."data/synchro/webToAndroid/";
if (mkdir($dir, 0777, true)) {
... // creating text files with data inside the webToAndroid folder
} else {
echo "cannot create";
}
At first run of the script the directory is created , but when I rerun the script then the code execution goes to the else block !
So how to make the mkdir always succeed ?
Do this:
define('RP_MAIN', $_SERVER['DOCUMENT_ROOT'] . 'impots/');
$dir = RP_MAIN."data/synchro/webToAndroid/";
if(is_dir($dir)){
echo 'directory already exists';
}
else if (mkdir($dir, 0777, true)) {
... // creating text files with data inside the webToAndroid folder
} else {
echo "cannot create";
}
I can't make a new directory in my web server. I think my code is ok to create a directory. Can you tell me what is the error ?
$path = "https://wwww.domain.com/astuces/uploads/products/".$id;
if(!is_dir($path)){
mkdir($path);
if(mkdir($path)){
echo "mkdir is created successfully";
}else{
echo "directory is not created";
echo mysql_error();
}
}
It's always showing me "directory is not created".
This is not just a permission issue, you are doing impossible things.
$path = "https://wwww.domain.com/astuces/uploads/products/".$id;
You can never use a url as the path to create a directory in a server.
The path should be the path in your web server like:
$path = "/path/to/your/project/astuces/uploads/products/".$id;
And then make sure the apache user has the permission.
If the the parent directory also not exist at first, you have to set the third parameter to true of mkdir:
if(mkdir($path, 0755, true)){
Try this code
$path = "https://wwww.domain.com/astuces/uploads/products/".$id;
if(!is_dir($path)){
$old_umask = umask(0);
mkdir($path, true);
chmod($path, 0777);
umask($old_umask);
if(mkdir($path)){
echo "mkdir is created successfully";
}else{
echo "directory is not created";
echo mysql_error();
}
}
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