-1

I am having a problem in getting a image uploading to the server (first I convert it to .png and resize it):

In functions.php

function imageUploadedResize($source, $target,$nWidth, $nHeight)
{

   $sourceImg = Imagecreatefromstring(file_get_contents($source));
   if ($sourceImg === false)
   {
      return false;
   }
   $width = imagesx($sourceImg);
   $height = imagesy($sourceImg);
   $targetImg = imagecreatetruecolor($nWidth, $nHeight);
   imagecopyresized($targetImg, $sourceImg, 0, 0, 0, 0, $nWidth,$nHeight,$width, $height);
   imagedestroy($sourceImg);
   imagepng($targetImg, $target);
   imagedestroy($targetImg);
}

In uploadtoServer.php

if(isset($_POST["fileToUpload"]))
    {
    $target_dir = "img/data/";
    $fn = $_FILES['fileToUpload']['tmp_name'];
    $newFileName = mysqli_insert_id($link).".png";
    header('Content-type: image/png');
    imageUploadedResize($fn, $target_dir.$newFileName, 45,60);
    }

If I change $fn to a static image like "https://pbs.twimg.com/profile_images/54789364/JPG-logo-highres.jpg" it works so I guess I am having a problem in $fn. Any thoughts what can it be?

Exprove
  • 1,301
  • 2
  • 18
  • 32
  • 2
    A good reason why you shouldn't suppress error messages by prefixing the functions with `@` – Devon Bessemer Jul 18 '16 at 17:11
  • 2
    Remove the `@` character from your code. We cannot help you if you don't know what the problem is. – Sverri M. Olsen Jul 18 '16 at 17:11
  • 2
    Have you enabled [PHP error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php)? And if so, what errors are you getting? – devlin carnate Jul 18 '16 at 17:11
  • PHP dont retrieve any errors. Sorry forgot to say that. I am using visual studio code so it haves syntax errors reporting. – Exprove Jul 18 '16 at 17:14
  • @Exprove, did you actually try it without the `@` or did you just remove that from your question? – Devon Bessemer Jul 18 '16 at 17:15
  • I tried it. No errors nor warnings. – Exprove Jul 18 '16 at 17:16
  • 2
    `isset($_POST["fileToUpload"])` I assume should reference `$_FILES` instead of `$_POST`? – Jonathan Kuhn Jul 18 '16 at 17:24
  • All your file path declarations should be **absolute** not *relative* (they may work as relative but it's a likely contender for cause of failure if your relative file structure and location is not exactly correct.). – Martin Jul 18 '16 at 17:26
  • @JonathanKuhn Hmm if i place $_FILES instead of $_POST it doesn't go inside the if. I can't acessing to $_FILES, if I print_r($_POST) with values it assigns them but if I print_r($_FILES) there is nothing there with file uploaded or not... Any Ideas? Server configuration? – Exprove Jul 18 '16 at 17:42
  • Can you post your HTML form? – Luca Jung Jul 18 '16 at 17:47

1 Answers1

2

Part 1:

Form input

As Johnathan mentioned, you're checking isset($_POST["fileToUpload"]) when a file upload form will supply the value in the $_FILES array. So you need to change your reference from $_POST to $_FILES otherwise it will always return false.

Edit: As your comment states you are getting 100% false, you should use a more specific qualifier such as if($_FILES['fileToUpload']['error'] == 0) .

Your HTML form submitting the data needs to be enctype="multipart/form-data" otherwise your server will not collect anything file shaped from your form. HTML forms also need to be set in the POST method.

Part 2

File storage:

Your references to file system storage for the image, such as $target_dir = "img/data/"; are relative, so that means that the value of $target_dir needs to be the child of the file that is calling that script block.

It would probably help you a lot to use Absolute file system references, typically (but not exclusively) using $_SERVER['DOCUMENT_ROOT']."/img/data"; This example will store data in the www.your-web-site.com/img/data location.

Part 3

Displaying the image.

You have a header setting content type to image/png but you do not actually output the image, instead saving it to the file reference mentioned above. If you want to output the image to the browser, you need to skip saving it so:

imagepng($targetImg, NULL); 

This will then output the image in $targetImg straight out to the browser. At the moment you appear to be saving it but not displaying it, despite setting the image header which means that any output to browser is expected to be an image content.

Some systems have a restriction on freshly uploaded data and prevent that data being handled too freely, to get around this it is advisable to use move_upladed_file to save the file to a "proper" destination and then access it to output to the browser.

Further Notes:

  • You should be checking $_FILES['fileToUpload']['error'] before acting on the file, as this will tell you if a file is present. using isset is a bad (very ambiguous) idea on this as the array can still be set, even if the file has not been uploaded.

  • You should use PHP error logging. You should not use error supression @.

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • Thanks for your answer! Did everything but completely forgot enctype="multipart/form-data"... – Exprove Jul 18 '16 at 17:49
  • I had almost forgotten it too, but the clue was your comment that there was no `$_FILES` data on the receiving page. Glad you got it fixed :) – Martin Jul 18 '16 at 17:53