PHP cannot open ghostscript generated files - php

I have a small php function which uses ghostscript to take a multipage pdf and convert it to multiple pngs. This part of the script works perefctly i can run it and check the images folder and they are all there.
The next part of the script resizes the pngs produced by ghostcript. However any attempt to make use of these pngs in php results in "failed to open stream: No such file or directory"
Moreover if copy one of the generated pngs to another folder or even the same folder it will become readable. This leads me to believe the ghostscript generated images might have the incorrect permissions. But when i run my script to check for permissions and if the file exists. both return false.
Here is the conversion script:
<?php
//Upload pdf convert to pngs in the folder jpgs/originals
$uploaddir = 'uploadtemp/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
exec('bin\gswin32c.exe -o jpgs\originals\%3d.png -sDEVICE=png16m -r150 uploadtemp\input.pdf');
unlink('uploadtemp\input.pdf');
header('Location: main.php');
} else {
echo "Upload failed";
}
?>
Here is the script which tries to call the now generated pngs:
<?php
$filename = "jpgs/originals/1.png";
if (is_writable($filename)) {
echo 'The file is writable';
} else {
echo 'The file is not writable';
}
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
?>
Both if statements return false even though i can see the files generated in the folder

This is certainly a weird problem, when running the ghostscript parameter :jpgs\originals\%3d.png it creates files with 2 spaces in front of the digit. So calling the file 2.png doesn't work because it doesn't exist but if you call [space][space]2.png it works! And when windows copies the file it must erase the extra spaces or something.

Related

php delete file with unlink()

I have read a lot of pages about this but I just can't get it to work.
I have a file on my server that needs to be deleted after the file has been imported.
The file is a .csv file and opened with fopen.
After import the file is closed with fclose.
Now I want to delete the file with:
chown($filepath, 666);
if (file_exists($filepath))
{
if (unlink($filepath))
{
echo "success";
}
else
{
echo "fail";
}
}
else
{
echo "file does not exist";
}
It keeps failing with file does not existbut I don't know why.
I use the exact same path to import
M.
You cant directly access the file using URL
$filepath = 'http://www.xxx.nl/files/testing.csv';
To access your file, use
$filepath = ./files/testing.csv
so file structure would be
- file
- testing.csv
- index.php

PHP File Upload / Host System

I have currently made a script which allows uploading for files within our company. I am looking to improve this script to allow for the directory of the uploaded file to masked within the download link (Don't really want people knowing the directory structure) Is there a way I can encode the link and then once a successful upload is made echo out the masked URL available for our clients to download.
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
//This is our size condition
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
Thanks Guys :)
I won't give you the code, but here is my strategy for implementing file sharing without giving away their location.
Create a file that takes as a parameter the name of the file to be retrieved. It's only purpose is to open the file and stream it out. It should know ahead of time what to do with the file, where to look for it, and how to return it(stream). Let's call it getFile($filename)
In your main PHP file, call the function and store the result in a variable:
$file = getFile($_POST['filename']);
Now send the user the file, and he will be none the wiser about where it came from.
You could make a script that the file bounces. It just accepts the filename as parameter. Something like:
download.php?file=myfile.txt
that way you don't say where you put your files, and it's easy to implement. You could add headers to actually download the file.

Why is move_uploaded_file not working?

Whenever I try to move a file it does not work and shows "Image file not uploaded"... I just want to know where the error is...
$target = '/var/www/student/public/myimage.jpg';
$destination = '/var/www/student/public/images/myimage.jpg';
if( move_uploaded_file( $target, $destination ) ) {
echo "Image file is successfully loaded";
} else {
echo "Image file not uploaded.";
}
I have checked error log (tail -f /var/log/apache2/error.log) but found nothing.
target and destination both directories have 777 permissions.
Can someone tell me that how to find out the error. Any idea ?
If you are not using HTTP POST upload method then you can use rename()
rename($target, $destination);
Has the file been uploaded in the current request?
move_uploaded_file will refuse to move files that are not uploads. (i.e. $target must equal $_FILES[$field_name]['tmp_name']
If it has been uploaded previously, move_uploaded_file will refuse to work (if it is even still there - PHP will delete it if you don't handle the file on that upload if I remember correctly)
If it is in fact not a file that has been uploaded with this request you'll want to use rename
move_uploaded_file() only works on http post files. http://php.net/manual/en/function.move-uploaded-file.php
to move a file already on the server, you will have to copy the file and unlink the old file
$target = '/var/www/student/public/myimage.jpg';
$destination = '/var/www/student/public/images/myimage.jpg';
if (copy($target, $destination)) {
unlink($target);
} else {
echo "Unable to copy $target to $destination.";
}

PHP-- filtering uploaded files to imags

how can i make sure that no php/html files are uploaded to my server? this is my code i have so far but it isn't working.
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
//This is our size condition
if ($uploaded_size > 35000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded and will be revied by moderators. You will recieve points based on the review.";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
Your code uses variables which are not set, for example, $uploaded_size which will be NULL unless you do something like...
$uploaded_size = $_FILES['uploaded']['size'];
Also, checking the MIME is not too great at telling you whether the file has PHP or not. It just means it has the php extension (that is if you are inspecting type in $_FILES).
For security, move uploads outside of the docroot, rename and drop any extension (to prevent Apache trying to run any malicious file). The original filename and type can be stored safely in a database, with a reference to the (perhaps hashed) new name.
You may also want to ensure if you are streaming the content later to always echo the content using readfile() and not something like include (which will run your PHP code, even if embedded in an image with image/gif MIME, which can be told it is a GIF if it includes the GIF header).
Check out http://www.php.net/manual/en/function.exif-imagetype.php - this checks for certain magic numbers that all JPG's have at the beginning. Also, as others have pointed out, you're using undefined variables... check out the PHP tutorial for file uploading ( which also documents the contents of $_FILE).
http://www.php.net/manual/en/features.file-upload.post-method.php

Why is my upload-of-a-file code putting 2 sets of files into my directory? (PHP)

When I upload a file using this code, it puts a copy of the file in the "uploads" folder(which is what I want) but it also puts a copy in my root. I only want the files going to the uploads folder.
define ('GW_UPLOADPATH', 'uploads/');
$upfile= GW_UPLOADPATH . $_FILES['userfile']['name'];
if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{
if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile)) //this is saying if the file isn't moved to $upfile.
{
echo 'Problem: could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: '; //this could be an attack b/c it might be from localhost.
echo $_FILES['userfile']['name'];
exit;
}
echo 'File uploaded successfully<br><br>';
What would be your temporary dir? Is it possible that somehow the uploaded file lands in the root but PHP can not delete it? Figuring this out requires a lot more knowledge about your setup.

Categories