Jump to content

Upload image to an image folder


matt.sisto

Recommended Posts

I want to modify this script so I can upload to a image folder, any advice please:

 

<?php
  require "dbconn.php";
  $title = $_POST['title'];
  $imgdata = $_POST['imgdata'];
  $sql = "INSERT INTO pix VALUES (0,'".$title."','".$imgdata."')";
  $result = mysql_query ($sql, $connection)
    or die ("Couldn't perform query $sql <br />".mysql_error());
  header("Location: uploadimg.php");
  exit();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Upload Image</title>
</head>

<body>
</body>
</html>

 

Appreciate any help. 8)

Link to comment
https://forums.phpfreaks.com/topic/150626-upload-image-to-an-image-folder/
Share on other sites

This is my upload form:

<html><head>
<div id="upload">
<h2>Image Upload</h2>
<form enctype=multipart/form-data method=post action="upload1.php">
<input type=hidden name=MAX_FILE_SIZE value=100000>
<input type=hidden name=completed value=1>
<p>
<label>Image</label><input type="file" name="imgdata" value="<?=$row['imgdata']?>" /></p>
<p>
<label>Name</label><input type="text" name="title" value="<?=$row['title']?>" /></p>
<input type=submit value="Upload"></form><br>
</div>

</body>
</html>

Originally I had it set so that it would upload to a table within my database, but I had issues getting the images from the table and displaying them. So now I would like to sort it out so it uploads to a folder on my server.

PHP: Upload and Resize an Image

You have created a cool contact directory and you want to allow people to upload their own photos, or you want to create an image repository which you upload images and create thumbnails out of them. Whatever it is, you need to be able to upload images and resize them.

PHP has the ability to upload files such as documents or images using the multipart/form-data protocol, but how do you use this and how do you resize the images after they are uploaded?

What you need: 

PHP installed properly 
*NIX OS such as Linux 
djpeg, cjpeg and pnmscale UNIX utility programs (see below)


This script works only with JPEG images. Also I'll resize the uploaded image only if it is larger than 250x200. Else, I'll leave it as it is.

Create Upload Form
First we need to create the form to upload the image. The MAX_FILE_SIZE variable needs to be set to the maximum allowable file size (in bytes) for upload. This is set using a hidden field and for this example will set to 50,000 bytes (approx. 50 kb).



<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="25000"> Upload Image: <input type="file" name="imgfile">
<font size="1">Click browse to upload a local file</font>
<input type="submit" value="Upload Image">
</form> 
Process Uploaded Data
Next we need to process the uploaded information when it is submitted. I'll do it all on the same page checking for the REQUEST_METHOD to know if the page was POSTed to.

PHP uploads the file to a temp location on your server (defined in php.ini) It also includes the following:


Variable Name Description 
$imgfile temporary filename (pointer) 
$imgfile_name original filename 
$imgfile_size size of uploaded file 
$imgfile_type mime-type of uploaded file 

NOTE: imgfile is the name given on the form 

Before copying the file, we check that a malicious user is not trying to abuse the script by trying to work on files it should not be, such as /etc/passwd. We do this with the PHP function is_uploaded_file(). More detail about this function is at the PHP.net site. 

If is_uploaded_file returns TRUE, copy the file from its temp location to where you want it using the PHP copy() function.



if (is_uploaded_file($imgfile))
{
   $newfile = $uploaddir . "/" . $final_filename";
   if (!copy($imgfile, $newfile)) 
   {
      // if an error occurs the file could not
      // be written, read or possibly does not exist
      print "Error Uploading File.";
      exit();
   }
}


Re-Sizing the Uploaded Image
To resize the uploaded image we use the pnmscale function which scales images in the PNM format. We use djpeg to convert the JPEG images to PNM, and cjpeg to convert them back. Here's the code to convert, scale and write out a scaled JPG image.



/*== where storing tmp img file ==*/
$tmpimg = tempnam("/tmp" "MKPH");
$newfile = "$uploaddir/scaled.jpg";

/*== CONVERT IMAGE TO PNM ==*/
if ($ext == "jpg") { system("djpeg $imgfile >$tmpimg"); } 
else { echo("Extension Unknown. Please only upload a JPEG image."); exit(); } 

/*== scale image using pnmscale and output using cjpeg ==*/
system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$newfile");


The above functions are just the basics of the script. There are a few little things added to complete the script. Such as checking the file extension, handling where to copy the file, and changing the upload filename. 

Download the complete Script Source
The directory this script runs in (or the upload directory) must be writable by the server

can you also ambo kindly look at these links will help

 

 

posting images via a form.

http://uk.php.net/manual/en/features.file-upload.post-method.php

 

upload multiple images via a form link.

http://uk.php.net/manual/en/features.file-upload.multiple.php

 

common problems with uploading pics link

http://uk.php.net/manual/en/features.file-upload.common-pitfalls.php

 

error message explained.

http://uk.php.net/manual/en/features.file-upload.errors.php

 

put method support.

http://uk.php.net/manual/en/features.file-upload.put-method.php

 

gd for php, will help to do a lot more with uploading/designing files.

http://uk.php.net/gd

 

and handling file upload link.

http://uk.php.net/features.file-upload

 

mysql tutorial with php link

http://www.php-mysql-tutorial.com/wikis/php-tutorial/uploading-files-to-the-server-using-php.aspx

 

advance upload and resizing gd tutorial

http://www.theopensurgery.com/29/php-upload-and-resize-image-script/

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.