Jump to content

Simple image upload script that puts image into specified folder?


Chappers

Recommended Posts

Hi

I want to have an upload facility on a page of my site which lets users upload an image that will be stored in a folder on the server, so I can grab the pic when I want and host it on the page later. I did originally want something that instantly put the image on the page, but then thought better of it knowing the amount of porn pics that'd probably be uploaded just because they can.

I can't use an email form unfortunately, because the free host I'm with has safe mode on, and smtp (send_mail, or something) disabled to stop such things, for spam purposes - but this image upload idea should still be possible, no?

I've just thought, if this is possible, maybe I can get around the problem of not being able to use an email form by having a form that sends the info to a file, which I could then bring up (but not publicly list in the menu) and see all messages sent? I have MySQL available and am using it for a comment form at the moment for adding comments to pages. Only trouble is I have to go into PHPAdmin to delete messages, which I'd have to do with the email type form idea too I s'pose.

Anyway, any ideas welcome.

Thanks,
James
Link to comment
Share on other sites

Hey Chappers,

I understand your frustration, but asking for someone to write code for you on a forum just isn't right.  Have you tried Googling for a file handling script?  Try http://www.hotscripts.com and see if you can find something there. 

I'm not trying to be mean or rude, but at least give a shot at researching and writing something on your own.  However, if you have problems with coding it, then you should come here and ask questions.

-- Will
Link to comment
Share on other sites

Hi, no offense taken; I agree with what you say, and must admit I realised it myself shortly after posting and had planned to delete this topic, but as it was so late, I ended up going to bed. I didn't mean to come across as asking someone to write the code for me, but for some strange reason I had the silly belief that it would be an uncommon request and there wouldn't be such a thing around on the Internet, but that maybe someone had written one themselves and would care to share. I'm not sure why I thought this, because I've taken your advice and found many scripts. Not like me to ask first, Google after, so I apologise.

I'm now going to try adding a few things to the script, such as verifying the file is an image, etc., and adding muliple upload fields so more than one image can be uploaded at a time.

There is one question I have, though: all the scripts I have found upload the image to a directory. As I'd like also to try having a comment form that uploads messages to a file, is there a simple way I can change it from uploading to a directory, to inserting code into a .php file on the server? Or would I have to insert it into a MySQL table, and then use another page to display any messages from there?

Thank you,
James
Link to comment
Share on other sites

  • 7 months later...
[code]

<?php
class picture
{
      var $save_dir;                    //where file will be saved
      var $filename="spacer.gif";        //default file name initially
      var $error_message="";            //string to be output if neccesary
      var $width;                        //height of final image
      var $height;                      //width of final image

      function picture($save_directory, $file_array, $max_width, $max_height)
      {
              $this->save_dir = $save_directory;               
              $this->width =    $max_width;
              $this->height =  $max_height;

              //--change filename to time - make it unique
              $temp_filename = $file_array['name'];
              $ext = explode('.',$temp_filename);
              $ext = $ext[count($ext)-1];
              $temp_filename = time().".".$ext;

              //--check that it's a jpeg or gif
              if (preg_match('/^(gif|jpe?g)$/',$ext)) {
                      // resize in proportion
                      list($width_orig, $height_orig) = getimagesize($file_array['tmp_name']);
                      if ($this->width && ($width_orig < $height_orig)) {
                              $this->width = ($this->height / $height_orig) * $width_orig;
                      } else {
                              $this->height = ($this->width / $width_orig) * $height_orig;
                      }

                      $image_p = imagecreatetruecolor($this->width, $this->height);                       

                      //handle gifs and jpegs separately
                      if($ext=='gif'){
                          $image = imagecreatefromgif($file_array['tmp_name']);                           
                          imagecopyresampled($image_p, $image, 0, 0, 0, 0, $this->width, $this->height, $width_orig, $height_orig);
                          imagegif($image_p, $this->save_dir.$temp_filename, 80);
                      }
                      else
                      {
                          $image = imagecreatefromjpeg($file_array['tmp_name']);                           
                          imagecopyresampled($image_p, $image, 0, 0, 0, 0, $this->width, $this->height, $width_orig, $height_orig);                           
                          imagejpeg($image_p, $this->save_dir.$temp_filename, 80);
                      }

                      imagedestroy($image_p);
                      imagedestroy($image);

                      $this->filename=$temp_filename;

              }else{
                      $this->error_message.="<br> file is not a jpeg or gif picture <br>";
              }
      }
}
?>
[/code]
Link to comment
Share on other sites

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.