0

I want to know how to upload one image to 2 different folders. Let me give you an example:

I want to upload 1 image. When I upload the image the system will resize the image to 2 different resolutions, portrait with resolution 400x400 and landscape with resolution 1200x1200. After the resizing, the resized image with resolution 400x400 will be stored in folder portrait and the other image will be stored in folder landscape. I'm still confused how to the make that logic.

<?php 

    //This is the directory where images will be saved 
    $target = "uploads/potrait/"; 
    $target = $target . basename( $_FILES['photo']['name']); 
    $target2="uploads/landscape/";
    $target2 = $target2 . basename( $_FILES['photo']['name']);

     //This gets all the other information from the form 
      $test=$_POST['test']; 
      $desc=$_POST['desc'];
      $pic=($_FILES['photo']['name']);
      $loc=$_POST['location'];


     // Connects to your Database 
     mysql_connect("localhost", "root", "") or die(mysql_error()) ;
     mysql_select_db("selfie") or die(mysql_error()) ; 

     $filename = mysql_real_escape_string($_FILES['photo']['name']);
     //Writes the information to the database 
     mysql_query("INSERT INTO image_upload (category, description,image ,location)     VALUES     ('$test', '$desc','$pic','$loc')"); 


     //Writes the photo to the server 
     if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
     { 

     copy($target, $target2);
     } 
     else { 

     //Gives and error if its not 
     echo "Sorry, upload failed."; 
     } 
      ?>
Midas
  • 7,012
  • 5
  • 34
  • 52
antonscie
  • 149
  • 1
  • 11
  • [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – [Jay Blanchard](http://stackoverflow.com/users/1011527/jay-blanchard) – Jeff Puckett Jun 13 '16 at 22:06
  • @ksealey I would expect a function called `move_...()` to actually _move_ the physical file. So that a second call would fail... Can you really successfully call that function _twice_? Or do you have to use 'copy' once? – arkascha Jun 13 '16 at 22:11
  • so any suggest for resize the image to two different resolution guys? Please help guys – antonscie Jun 13 '16 at 22:20
  • 1
    Stop using MySQL and start using `MySQLi`. Now. – Martin Jun 13 '16 at 23:21
  • @Martin or better: PDO – Midas Jun 13 '16 at 23:23
  • 1
    @Midas while PDO is indeed an equal or better alternative to MySQLi, for the OP who is still using Procedural MySQL, jumping to the syntax of PDO is usually a step too far and this puts many people off making the change. So I try to promote `MySQL --> MySQLi --> PDO` logic. `:-)` – Martin Jun 13 '16 at 23:25

1 Answers1

0

This is what an image resize function could look like:

function save_resized_image($file_in, $file_out, $new_w, $new_h)
{
    list($w, $h, $type) = getimagesize($file_in);

    switch ($type) {
        case IMG_JPG: $src = imagecreatefromjpeg($file_in); break;
        case IMG_GIF: $src = imagecreatefromgif ($file_in); break;
        case IMG_PNG: $src = imagecreatefrompng ($file_in); break;
        default:
            return false;
    }

    $tmp = imagecreatetruecolor($new_w, $new_h);
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $new_w, $new_h, $w, $h);

    switch ($type) {
        case IMG_JPG: imagejpeg($tmp, $file_out); break;
        case IMG_GIF: imagegif ($tmp, $file_out); break;
        case IMG_PNG: imagepng ($tmp, $file_out); break;
    }
    return true;
}

You can use it like this:

$basename = basename($_FILES['photo']['name']);
$tmp_name = $_FILES['photo']['tmp_name'];

save_resized_image($tmp_name, "portrait/$basename", 400, 400);
save_resized_image($tmp_name, "landscape/$basename", 1200, 1200);

This function doesn't handle image aspect ratio. But I leave this up to you.

Note: no move_uploaded_file is needed here.

Midas
  • 7,012
  • 5
  • 34
  • 52