Jump to content

Recommended Posts

Hi

I'm trying to resize pictures for thumbnails

I can do this correctly at linux command line with the following command

convert ideologies.jpeg -resize 300x200 ./Thumbnails/testphp.jpg

However trying to wrap this in PHP doesn't seem to work

// Upload file to server
        if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
            // Insert image file name into database
            $insert = $conn->query("INSERT into Threads (Title, ThreadBody, filename) VALUES ('$Title', '$BodyText', '$fileName')");
            if($insert){
                $statusMsg = "The file ".$fileName. " has been uploaded successfully.";
exec("convert {['fileName']} -resize 300x200 wankertestphp.jpg");
            }

If you look above I do successfully hit the "The File has been uploaded successfully" line so it is entering that If statement.

I've also checked the Threads table with a Select * from Threads and I can see the $fileName variable is entering there.  I've tried numerous different ways around the $fileName variable (removing the brackets...removing the brackets and the apostrophes")

 

Any help is appreciated.

Couple issues here. First is the matter of putting variables into strings. "{['filename']}" is just not going to work at all. I mean, there's gotta be a $ somewhere. Second is that you have to use the right values for convert.

Because of the move_uploaded_file, you need to convert $targetFilePath. Not whatever "filename" was supposed to be.
Then, just like with variables in SQL queries, you need to make sure that the $targetFilePath doesn't cause problems with the convert command. There is no "prepared commands" concept like SQL has so you have to escape it yourself - fortunately there's a built-in function to do that.

$targetFilePathArg = escapeshellarg($targetFilePath);
exec("convert $targetFilePathArg -resize 300x200 ./Thumbnails/testphp.jpg");

 

Thanks I did try that and it reached the correct IF statement to produce "The file has been uploaded successfully"

 

However it did not create the file.

I even amended your statement to

$targetFilePathArg = escapeshellarg($targetFilePath);
exec("convert $targetFilePathArg -resize 300x200 .jpg");

Just to check it was not failing at finding the folder for some reason.

Bigger block of code below

// File upload path
$targetDir = "upload/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
    // Allow certain file formats
    $allowTypes = array('jpg','png','jpeg','gif','pdf');
    if(in_array($fileType, $allowTypes)){
        // Upload file to server
        if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
            // Insert image file name into database
            $insert = $conn->query("INSERT into Threads (Title, ThreadBody, filename) VALUES ('$Title', '$BodyText', '$fileName')");
            if($insert){
                $statusMsg = "The file ".$fileName. " has been uploaded successfully."; 
$targetFilePathArg = escapeshellarg($targetFilePath);
exec("convert $targetFilePathArg -resize 300x200 .jpg");
 }else{
                $statusMsg = "File upload failed, please try again.";
            } 

 

Edited by Fishcakes
5 minutes ago, Fishcakes said:

I even amended your statement to


$targetFilePathArg = pathinfo($targetFilePath,PATHINFO_EXTENSION);
exec("convert $targetFilePathArg -resize 300x200 testingresize.jpg");

That's very much not correct. Go back to what I posted.

You might be encountering file permission problems.

1. Decide what directory you want to put the thumbnails in. If you're using upload/ then I suggest either (a) upload/thumbnails or (b) changing the filenames to be like example.jpg for the original and example.thumb.jpg for the resized version, and they both go in upload/.
2. If you want a subdirectory, create it and then chmod 0777 it - or do whatever it is you did with upload/ to allow copying the file there. Because I'm 80% certain you did have to do something.

Also, checking if the $_FILES "name" isn't empty is not enough. Look at the "error" and make sure it has the value UPLOAD_ERR_OK. If not then maybe there was no file (UPLOAD_ERR_NO_FILE) or there was some other problem during the upload.

Yes I did chmod the folder.

After uploading my internet page displays the latest upload so I'm seeing it retrieved by PHP onto my site as well as seeing it in the folder.

What is the best way to error check in php? At the moment I'm just doing php upload.php to check it in linux terminal

For exec to work do I did a debian package installed?

8 minutes ago, Fishcakes said:

What is the best way to error check in php? At the moment I'm just doing php upload.php to check it in linux terminal

Best way to do what?

8 minutes ago, Fishcakes said:

For exec to work do I did a debian package installed?

Is that a question? Because no, I don't believe you have to install anything to use exec(): it's part of the core PHP and it's not something Linux distros separate out into installable packages (as opposed to stuff like JSON, which is "core" but often separated).

OK I added some error checking

$output=null;
$retval=null;
 exec("convert $targetFilePathArg -resize 300x200 testresize.jpg",$output, $retval);
echo "REturned with status $retval and output:\n" ; 
if ($retval == null) { 
echo "Retval is null\n" ; 

 

which returns

REturned with status 1 and output: The file test.jpg has been uploaded successfully.

status 1 would be an error no?

 

I also then went to create a php file with just this in

<?php 
  exec("convert arrow.png -resize 300x200 ./testresize.jpg",$output, $retval);
?>

Then run this at terminal and I get the following.

convert-im6.q16: unable to open image `testresize.jpg': Permission denied @ error/blob.c/OpenBlob/2874.

But I've already provided permissions to this folder via chmod -R 707 upload

937203654_screenshotofpermissiosn.png.d35c2b1689bc0968ef0f86b6d4848cef.png

Edited by Fishcakes
  • 1 year later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.