1

I got kinda weird problem, but hope someone knows what's the case.

I've cloned project from git and started to work with it in PHPStorm, ran it in my local Apache server. So it was working first time, but in my next try, script didn't response. restart Apache didn't help, as re-run script in IDE too. No any information in browser console/resources.

I've decided that this is a problem in headers, and re-wrote script:

$fileInputName = 'uploadedFile';
if ( !array_key_exists( $fileInputName, $_FILES ) ) {
    echo ('there are no files to work with');
    die();
}
$uploadName = $_FILES[$fileInputName]['tmp_name'];
$size = $_FILES[$fileInputName]['size'];
$pathToSaveSVG = realpath(dirname(__FILE__)) . '/SVG';

$fileName = $uploadName . '.pdf';
$targetName = $fileName . '.svg';

if ( !move_uploaded_file( $uploadName, $fileName ) ) {
    echo ($uploadName. '    '. $fileName);
    unlink( $uploadName );
    echo( 'can\'t move uploaded file' );
    die();
}

exec( 'pdf2svg ' . escapeshellarg( $fileName ) . ' ' . escapeshellarg( $pathToSaveSVG. '/' . $targetName ) );
unlink($fileName);

echo $pathToSaveSVG . '/' . $targetName;
die;

html:

<form action="pdf_to_svg.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
    <input type="file" name="uploadedFile" id="file" accept="application/pdf" />
    <input type="submit" name="submit" value="Convert " />
</form>

Then, it repeats. Script works fine if i just take files to convert by name, not by POST data sending.

Please, any hints, why sending data via POST method crashes script on second time working? It looks like cyclic forwarding at server-side or something, but with no visible errors, and in apache logs too.

ADD: Looks like it relates to permissions. At first time, file uploads with error 3 - file have uploaded partially. I tried chmod 755 -R ./SVG, no results. Could someone help me figure this out?

Community
  • 1
  • 1
Vladimir Tagai
  • 321
  • 1
  • 2
  • 13
  • Did you look into the browsers networking tool and saw something out of the ordinary (like browser is still waiting for response after maximum execution of script)? PS: echo is a language construct *not a function*, therefor you should remove the brackets ( ). – Charlotte Dunois Jan 17 '15 at 23:43
  • Also use shell_exec() instead of exec() and capture the response. Connect to server via SSH and use `top` to look if pdf2svg starts running. – Charlotte Dunois Jan 17 '15 at 23:45
  • @CharlotteDunois thanks, so I am not a php developer, but fix these things later. Browser writes just 'Waiting for localhost...', but absolutely no information in tools. – Vladimir Tagai Jan 17 '15 at 23:46

2 Answers2

0

Be sure your file upload form has attribute enctype="multipart/form-data"
otherwise the file upload may not work correctly.

Make sure you set memory_limit large enough.

Make sure you set max_execution_time large enough.

Make sure you set post_max_size large enough.

Check the core values:

phpinfo(); 

Qualify the upload had no errors and has the correct Content MIME Type

if( is_uploaded_file($_FILES['uploadedFile']['tmp_name']) || !($_FILES['uploadedFile']['error'] !== UPLOAD_ERR_OK) && strtolower($_FILES['uploadedFile']['type']) == 'application/pdf' )){

Get the returned status of the exec:

$status = exec(...);
echo "<p>$status</p>";

Make sure you destination path and file names are correct.

if (file_exists($fileName ) && file_exists($pathToSaveSVG)){
Misunderstood
  • 5,534
  • 1
  • 18
  • 25
  • as I mentioned minutes ago, it has error with code 3. At first time, it doesn't have 'tmp_name' key, and 'size' equals to 0. I already changed apache permissions like that: http://stackoverflow.com/questions/8457683/php-move-uploaded-file-fails-and-i-dont-know-why , but it still doesn't work. – Vladimir Tagai Jan 18 '15 at 00:30
  • @realvladi If www-data (user which runs the webserver) is not the owner of the directory, the directory needs a chmod of 766. http://en.wikipedia.org/wiki/File_system_permissions#Numeric_notation – Charlotte Dunois Jan 18 '15 at 00:43
  • Check memory, max file size, execution_time with `phpinfo()`. Check response header for `Content Type multipart/form-data` – Misunderstood Jan 18 '15 at 00:44
0

Found an answer. When files being upload to the server, apache tries to put file into temporary directory, like /var/tmp . www-data has rights to read and write in that directory, and this is why I got stuck. My project was located not in /var/www , and I ran it with PhpStorm tools. I was wondering, why it didn't work - www-data group has all permissions to project folder and temporary folder. But then, I tried in my script

echo exec('whoami');

and it returned not www-data, but my username. I simply changed /etc/group file

www-data:x:33

to

www-data:x:33:myUserName

and now it works.

Vladimir Tagai
  • 321
  • 1
  • 2
  • 13