Jump to content

Uploading images, creating URL and putting URL into database.


Anxious

Recommended Posts

Hello, to get an idea of how this would work. I used this tutorial.

http://www.webdeveloper.com/forum/showthread.php?t=101466

 

I have got this far.

This is the upload form

 <?php
if(isset($_SESSION['upsuccess'])){
   unset($_SESSION['upsuccess']);
   
   echo "<b><u>Photo Uploaded!</u></b>";
   echo "<p><b>$session->username</b>, your photo has been successfully uploaded.</p>"
   ."<a href=\"profile.php?user=$session->username\">My Profile</a>.</p>"
   ."<a href=\"photos.php?user=$session->username\">My Photos</a>.</p>"
   ."<a href=\"index.php\">Home</a>.</p>";
}
else{
?>
  <?php
if($session->logged_in){
?>
  <br>
  <strong>Upload Your Photos </strong> <br>
  <? echo $session->username; ?><br>
  <br>
  View Your Profile <?php echo "<a href=\"profile.php?user=$session->username\"><span class=\"style10\">Here</span></a>"; ?>  <br>
  View Your Photos <a href="photos.php" class="style10">Here</a>
  <?php
// make a note of the current working directory relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
// make a note of the location of the upload handler script
$uploadHandler = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'processor.php';
// set a max file size for the html upload form
$max_file_size = 100000000; // size in bytes
?>  
  <form id="Upload" action="<?php echo $uploadHandler ?>" enctype="multipart/form-data" method="post">    
    <h1>
      <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">
    <? echo $form->error("file"); ?>    </h1>        
        <p>
            <label for="file">File to upload:</label>
            <input id="file" type="file" name="file">
        </p>                
        <p>
            <label for="submit"></label>
            <input name="subupload" type="hidden" id="subupload" value="1">
            <input id="submit" type="submit" name="submit" value="Upload!">
        </p>   
  </form>

This goes to process.php which is as follows.

 

<?php    function procUpload(){
   global $session, $form;
   /* Upload Photo Attempt */
   $fetval = $session->uploadphoto($_POST['file']);
   
         /* Account edit successful */
      if($retval){
         $_SESSION['useredit'] = true;
         header("Location: ".$session->referrer);
      }
      /* Error found with form */
      else{
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: ".$session->referrer);
      }
   } ?>

This then goes to uploadphoto in session.

This is uploadphoto

 

<?php     function uploadphoto($subfile){
      global $database, $form; //The database and form object

// make a note of the current working directory, relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
// make a note of the directory that will recieve the uploaded file
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'profilephoto/';
// make a note of the location of the upload form in case we need it
$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'uploadphoto.php';
// make a note of the location of the success page
$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'success.php';
// fieldname used within the file <input> of the HTML form
$fieldname = 'file';

if($subfile){
  $field = "file";
  if(!$subfile){
  $form->setError($field, "* File Path not selected");
  }
  
  @is_uploaded_file($_FILES[$fieldname]['tmp_name'])
      or $form->setError($field, "*not an HTTP upload");
  
  @getimagesize($_FILES[$fieldname]['tmp_name'])
      or $form->setError($field, "* only image uploads are allowed");
  }

  /* Upload Photo */
      if($subfile){
         $database->updateUserPhoto($this->username,"profilephoto",$profilephoto);
      }
} ?>

This then goes to database and puts the values into the database and SHOULD create a URL to the image.

 

<?php    function updateUserPhoto($username, $field, $value){
      $q = "UPDATE ".TBL_PHOTO." SET ".$field." = '$value' WHERE username = '$username'";
      return mysql_query($q, $this->connection);
  
  $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'profilephoto/';

  $now = time();
      while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
   {
      $now++;
   }
}
?> 

 

As a summary of all of the above.

The upload form gets submitted, and goes to process.php that processes the values and sets them. Then goes to session.php to check for errors, and then goes to database.php to enter the values.

 

However, I am not sure. What should be entered into the database is the URL to the image.

As far as I have got, it uploads the image to the database, and should upload the image to the website under the directory 'profilephoto'

 

 

Now my problems

I need to set some other error fields in session.php, the max file size should be 5MB, the size of image should be 100 x 100 pixels maximum (Make it easy to change it if I needed to). Check if its an image, and not a document or other, And to bring up the appropiate error fields for each.

 

 

Going by the tutorial, I did quite a bit, but no idea if any of it will work.

I will be needing some help on this.

 

Any help is greatly appreciated, maybe to correct the database, so that it uploads the image onto the site, and enters the URL into the database table.

 

Thank you so much

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.