I have the following bit of code that is giving me problems.
#$fullfilename="/data/extract/".$curpkg."/".$curfilename;
$fullfilename = "/tmp/test.txt";
$readline = 0;
$lictext="";
try {
$file = new SplFileObject($fullfilename);
$readline=$curline-1;
while ($readline <= ($curline -1 + $curlinecount)) {
$file->seek($readline);
$lictext = $lictext . $file->current()."\n<br>";
$readline = $readline + 1;
}
} catch (Exception $e) {
$lictext = "couldn't open it $fullfilename<br> Exception: $e<br>";
}
When I use the currently uncommented $fullfilename variable declaration, it works fine, but when I use the code that is commented out it does not. I get the following error:
couldn't open it /data/extract/test.txt
Exception: exception 'RuntimeException' with message \
'SplFileObject::__construct(/data/extract/test.txt):\
failed to open stream: No such file or directory' in \
/srv/www/htdocs/legal/index.php:70
Stack trace:
#0 /srv/www/htdocs/legal/index.php(70): \
SplFileObject->__construct('/data/extract/test.txt')
The only difference is that the data I want to use is a separate drive mounted at /data.
Permissions for the entire structure are 777: drwxrwxrwx 2 root root 53248 Jan 7 14:31 data.
I am at a loss here, I have the same problem with file_exists() and is_readable(). Can anyone give me some guidance here?
Related
I use this clear.php script that remove all files and folders in correct clear directory:
<?php
function deleteDir($dirPath) {
if (! is_dir($dirPath)) {
throw new InvalidArgumentException("$dirPath must be a directory");
}
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
$dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file)) {
deleteDir($file);rmdir($file);
} else {
unlink($file);
}
}
}
deleteDir('clear');
?>
when i run the code mydomain.com/clear.php it work and remove all files and folders but when i create a Scheduled Tasks in plesk and use this code it shows:
Task "httpdocs/clear.php" completed with error in 0 seconds. See
details
and when i click the see details it shows:
Task "httpdocs/clear.php" completed with error in 0 seconds, output:
PHP Fatal error: Uncaught InvalidArgumentException: clear must be a directory in /var/www/vhosts/domain_name.com/httpdocs/clear.php:4
Stack trace:
#0 /var/www/vhosts/domain_name.com/httpdocs/clear.php(19): deleteDir('clear')
#1 {main}
thrown in /var/www/vhosts/domain_name.com/httpdocs/clear.php on line 4
I'm using a VM deployed with windows server r2 2012 and facing an issue while converting PPT to Image using PHP COM dll on IIS8.Below is the code that I'm using
<?php
$file_path = "test_ppt_to_image.pptx";
$powerpnt = new COM("powerpoint.application") or die("");
try {
$presentation = $powerpnt->Presentations->Open(realpath($file_path)) or die("Unable to open the slide");
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";exit;
}
$i=1;
$exportFolder = realpath("test_images");
foreach($presentation->Slides as $slide)
{
$slideName = $i."_SS_";
$slide->Export($exportFolder."/".$slideName.".jpg", "jpg");
$i++;
}
$powerpnt->quit();
?>
below is the error from catch
Error : [13-Apr-2016 23:43:30 America/Los_Angeles] PHP Fatal error: Uncaught exception 'com_exception' with message 'Source: Unknown
Description: Unknown' in C:\inetpub\wwwroot\ppttoimage.php:5
Stack trace:
0 C:\inetpub\wwwroot\ppttoimage.php(5): variant->Open('C:\inetpub\wwwr...')
1 {main}
thrown in C:\inetpub\wwwroot\ppttoimage.php on line 5
I tried giving permissions from command "dcomcnfg" for DCOM extensions and no luck still getting the same error.
Can anyone plz help me with some solution. Thanks in advance.
Using phar to create tar.gz archive returns a strange error, the following is the error message:
exception 'BadMethodCallException' with message 'Unable to add newly
converted phar
"c:/www/dimg/uploads/7e6d3a5e39e43d1351e7069517f11250.tar.gz" to the
list of phars, a phar with that name already exists' in
c:\www\dimg\upload.php:163 Stack trace:
0 c:\www\dimg\upload.php(163): PharData->compress(4096)
1 {main}
The snippet to produce Phar archives am using:
$dir_id = md5(microtime() . $_SERVER['REMOTE_ADDR']);
$upload_dir = 'uploads/' . $dir_id;
mkdir($upload_dir, 777);
try {
$a = new PharData($upload_dir . '.tar.gz');
$a->buildFromDirectory($upload_dir);
$a->compress(Phar::GZ);
} catch (Exception $e) {
$error = true;
$err_msg .= '<li>Exception : ' . $e . '</li>';
}
I tried to empty uploads directory but the same error is produced each time.
Instead of using
$a = new PharData($upload_dir . '.tar.gz');
Use:
$a = new PharData($upload_dir . '.tar');
Actually compress generates two .tar and then tar.gz. Since you have specified .tar.gz as the initial output, it cannot overwrite it with the same file type.
I believe the problem is that you are trying to build the directory that the tar.gz is being sent to. In other words, you are telling it to build to a particular directory, and then telling it to build that directory.
All you should need to do is build the tar.gz in another directory than the one you are trying to compress.
I am trying to loop through this directory:
$path = "D:\\import\\statsummary\\";
Here is my code:
$path = "D:\\import\\statsummary\\";
//$path = "C:\\test";
//function load_csv($path, $filename){
if(is_null($filename)){
header('Content-type: text/plain');
$output = array();
foreach (new DirectoryIterator($path) as $file){
if($file->isFile()){
$output[] = $i++ . " " . $file->getFileName() . "\n";
$output[] = file($file->getPathName());
$output[] = "\n------------\n";
}
}
}
echo implode('', $output);
When I run this script, I get this error:
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'DirectoryIterator::__construct(D:\import\statsummary\,D:\import\statsummary\): Access is denied. (code: 5)' in C:\inetpub\wwwroot\include\file_importer.php:10
Stack trace:
#0 C:\inetpub\wwwroot\include\file_importer.php(10): DirectoryIterator->__construct('D:\import\...')
#1 {main}
thrown in C:\inetpub\wwwroot\include\file_importer.php on line 10
But when I change it to a test directory on my C:\ drive, it runs just fine. I've even created a username to run PHP as directed in this post:
php - Unable to connect to network share - Stack Overflow
Based on the DirectoryIterator class, something like this should work:
<?php
$path = "D:/import/statsummary";
$output=array();
$iterator = new DirectoryIterator(path);
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
$filename= $fileinfo->getFilename();
$path=$fileinfo->getPathname();
$output[][$filename]=$path;
}
}
print_r($output);
?>
Update
Since you're getting access denied, you'll need to run the command prompt (CMD) window as Administrator more than likely. If this is on a link (lnk) you can change the permissions in the link settings.
For instance if you right-click on the shortcut for cmd as select properties, you would go to shortcut>advanced>Run as Administrator.
So, i've read this question about move_uploaded_file() problems. However, on my apache-powered localhost lamp stack, its working just fine. So i think it may be a filesystem / path thing, and not a code thing.
when uploading files to my site locally, it works.
but when I'm on the QA server (which is nginx powered), i get this error:
2012/09/08 15:34:21 [error] 11794#0: *5187 FastCGI sent in stderr: "files not empty
PHP Warning: move_uploaded_file(/var/www/qa.mysite.com/mysite/app/files/maps-aprilfools.tiff): failed to open stream: No such file or directory in /var/www/qa.mysite.com/mysite/app/models/files.php on line 516
PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpvdtznP' to '/var/www/qa.mysite.com/mysite/app/files/maps-aprilfools.tiff' in /var/www/qa.mysite.com/mysite/app/models/files.php on line 516" while reading response header from upstream, client: 72.xxx.xxx.xxx, server: qa.mysite.com, request: "POST /projects/3/files HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "qa.mysite.com", referrer: "http://qa.mysite.com/projects/3/files"
and this is the code that I wrote to handle uploading a file:
public function fileUploadToProject( $project_id ) {
if ($_FILES["upload-file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
} else {
$dbSuccess = false;
$tmp_name = $_FILES["upload-file"]["tmp_name"];
$name = $_FILES["upload-file"]["name"]; // someFile.ext
$size = $_FILES['upload-file']['size']; //size in bytes
$mime = $_FILES['upload-file']['type']; //size in bytes
$destination = __FILES__.'/'.$name;
$uploaded = move_uploaded_file( $tmp_name, $destination );
// add entry to database.
/*
* null because there is no container yet.
* We're only uploading to local
*/
$user_container_name = null;
$uploaded_by = LoggedInUser::get_user_id();
/*
* Set this 1 when we're not dealing with our external fileserver
*/
$isLocal = 1;
/*
* Probably shouldn't do this forever for storage size reasons, but for now its useful.
*/
$localPath = $destination;
$task_id = null;
if( $uploaded ) {
$dbSuccess = $this->insertFileRefService( $task_id,
$project_id,
$user_container_name,
$name,
$mime,
$size,
$uploaded_by,
$isLocal,
$localPath
);
if($dbSuccess) {
return true;
} else {
// should I rollback / delete that file?
return false;
}
} else {
return false;
}
}
}
So, is there anything I should know about moving temp files to my filesystem with on nginx? or do you think it is simply a path problem? code problem?
Also, please note that line 516 is this: $uploaded = move_uploaded_file( $tmp_name, $destination );
The folder had problems with permissions, among other things.
the /files directory in that path actually doesn't exist. I somehow never realized that the folder simply wasn't there. Whoops.
then, i had to determine what user was used by nginx to execute php:
ps aux | grep "nginx"
then i had to chown the files directory:
chown -R root:userFromStep1 files
then i had to chmod the directory:
chmod g+w files
that worked like a charm.
Can please try with giving the exact path instead of using the __FILES__. Because it'll not be nginx problem. it will be path problem only