upload and resize and image with PHP

Posted on 2007-10-16
Last Modified: 2013-12-13
I am trying to upload an image to a folder and resize it to 400px by 300px on the way. At the moment it just upload the full size image to my folder. Can anybody help?


//home many images are in the table
$result = mysql_query("SELECT * FROM images WHERE property_id='$property_id'");
$num_rows = mysql_num_rows($result);

mkdir("../property_images/$property_id", 0700);


//upload the images

if(isset($_POST['_upload'])  && $_FILES['userfile']['size'] > 0)

$target_path = "../property_images/$property_id/".basename( $_FILES['userfile']['name']);

$img_url= $property_id."/".basename( $_FILES['userfile']['name']);

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {
  /*  echo "The file ".  basename( $_FILES['userfile']['name']).
    " has been uploaded";*/
} else{
    echo "There was an error uploading the file, please try again!";
Question by:roscoeh23
    LVL 15

    Expert Comment

    this php class can suite your requirement
    LVL 5

    Accepted Solution

    // Configuration
    $uploaddir = "resize/";

    * Create upload Directory
    if (!is_dir("resize")) {
          if (!mkdir($uploaddir))
                die ("upload_files directory doesn't exist and creation failed");
          if (!chmod($uploaddir,0755))
                die ("change permission to 755 failed.");

    if(isset($_POST['upload'])) {
          $image_name = $_FILES['file']['tmp_name'];
          $new_side = 100;

          // Get new dimensions
          list($width, $height) = getimagesize($image_name);

          $w = $width;
          $h = $height;

          if ($h > $w) {
                $new_width = ($new_side / $h) * $w;
                $new_height = $new_side;      
          } else {
                $new_height = ($new_side / $w) * $h;
                $new_width = $new_side;

          // Resample
          $image_p = imagecreatetruecolor($new_width, $new_height);
          $image = imagecreatefromjpeg($image_name);
          imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
          $upload_image = $_FILES['file']['name'];
          imagejpeg($image_p, "$uploaddir$upload_image");

    $filelist = "";
    $pattern="(\.jpg$|\.jpeg$|\.gif$)"; //valid image extensions
    while ($file = readdir($handle)) {
          if(!is_dir($file) &&!is_link($file)) {
                if(eregi($pattern, $file)) {
                      $filelist .= "<img src='$uploaddir$file'>";
                      $filelist .="<br>";

                <title>Resize Image</title>
                <form action="resize.php" method="post" ENCTYPE="multipart/form-data">
                      File: <input type="file" name="file" size="30">
                      <input type="submit" name="upload" value="Upload!">

                            <td><?php echo $filelist ?></td>

    Expert Comment

    What could be the case if the accepted solution script above and similar like are crashing my server? I am using the code above that "siva siva" entered.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now