[SOLVED] Uploading

This is a discussion on "[SOLVED] Uploading" within the PHP Forum section. This forum, and the thread "[SOLVED] Uploading are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Oct 6th, 2007, 15:49
Highly Reputable Member
Join Date: Dec 2006
Location: Norwich
Posts: 721
Blog Entries: 4
Thanks: 6
Thanked 2 Times in 2 Posts
[SOLVED] Uploading

How would i get it so a user could use the form to upload a image to dir: images. Using the same for to add it to the DB



Quote:
<form method="post" name="form2" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td nowrap align="right">Name:</td>
<td><input type="text" name="Name" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Dis:</td>
<td><input type="text" name="Dis" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">By:</td>
<td><input type="text" name="By" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">ImageURL:</td>
<td><input type="text" name="ImageURL" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">&nbsp;</td>
<td><input type="submit" value="Insert record"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form2">
</form>
PHP: Select all

<?php
function GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}

$editFormAction $_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset(
$_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
  
$insertSQL sprintf("INSERT INTO images (Name, Dis, `By`, ImageURL) VALUES (%s, %s, %s, %s)",
                       
GetSQLValueString($_POST['Name'], "text"),
                       
GetSQLValueString($_POST['Dis'], "text"),
                       
GetSQLValueString($_POST['By'], "text"),
                       
GetSQLValueString($_POST['ImageURL'], "text"));

  
mysql_select_db($database_localhost$localhost);
  
$Result1 mysql_query($insertSQL$localhost) or die(mysql_error());

  
$insertGoTo "/admin/image.php";
  if (isset(
$_SERVER['QUERY_STRING'])) {
    
$insertGoTo .= (strpos($insertGoTo'?')) ? "&" "?";
    
$insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  
header(sprintf("Location: %s"$insertGoTo));
}

mysql_select_db($database_localhost$localhost);
$query_Recordset1 "SELECT ImageID, ImageURL, `By` FROM images";
$Recordset1 mysql_query($query_Recordset1$localhost) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 mysql_num_rows($Recordset1);
?>
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Oct 6th, 2007, 16:14
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Uploading

Here's the code for a simple upload script:
Code: Select all
<form name="upload_form" action="<?php echo($link_prefix); ?>fileshare/upload/" method="post" enctype="multipart/form-data">
     <input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
     <p><input type="file" name="uploadfile" /></p>
     <p><input type="submit" action="submit" value="upload" /></p>
</form>
PHP: Select all

if(isset($_FILES['uploadfile'])) { // check for submitted file
            
$message='';
            switch(
$_FILES['uploadfile']['error']) {
                case 
UPLOAD_ERR_OK:
                    if(
move_uploaded_file($_FILES['uploadfile']['tmp_name'],$real_path.$_FILES['uploadfile']['name'])) {
                        
$message.='Upload successful.';
                    } else {
                        
$message.='Upload failed.';
                    }
                    break;
                case 
UPLOAD_ERR_INI_SIZE:
                case 
UPLOAD_ERR_FORM_SIZE:
                    
// file too large
                    
$message.='File is too large.';
                    break;
                default:
                    
// file upload failed
                    
$message.='Upload failed.';
                    break;
            }
        } 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Oct 6th, 2007, 16:40
Highly Reputable Member
Join Date: Dec 2006
Location: Norwich
Posts: 721
Blog Entries: 4
Thanks: 6
Thanked 2 Times in 2 Posts
Re: Uploading

I don't think i explain it well. My site users will upload images ( cartoons). I would like the php to upload to the image dir. AND add to the DB the record set is in the first post.
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)

Last edited by simonb; Oct 6th, 2007 at 17:11.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Oct 6th, 2007, 17:29
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Uploading

Yeah, you'll have to combine the two scripts and that should be exactly what you want!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Oct 6th, 2007, 17:31
Highly Reputable Member
Join Date: Dec 2006
Location: Norwich
Posts: 721
Blog Entries: 4
Thanks: 6
Thanked 2 Times in 2 Posts
Re: Uploading

Ok right thanks.

How do you do that. lol.
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Oct 6th, 2007, 17:42
Highly Reputable Member
Join Date: Dec 2006
Location: Norwich
Posts: 721
Blog Entries: 4
Thanks: 6
Thanked 2 Times in 2 Posts
Re: Uploading

Quote:
Originally Posted by saltedm8 View Post
<?php include 'yourfile.php';?>
how does that help me.
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Oct 6th, 2007, 19:46
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Uploading

How do you reference to the image that has been uploaded in the database......do you insert the filename in the database and reference to that filename.

I have a script where I upload a file and I insert the filename in the database
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Oct 6th, 2007, 19:47
Highly Reputable Member
Join Date: Dec 2006
Location: Norwich
Posts: 721
Blog Entries: 4
Thanks: 6
Thanked 2 Times in 2 Posts
Re: Uploading

insert the filename in the database and reference to that filename.


Yeah thats what i am looking for.
If i could have a copy.
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)

Last edited by simonb; Oct 6th, 2007 at 19:59.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Oct 6th, 2007, 19:58
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Uploading

This is how I upload an image but this script resizes it to a thumbnail but you don't have to do that or you can just change the image dimensions in $twidth and $theight

Where I have <select name="gallery"> this is where I choose which gallery I want to upload to (even though it all uploads to same directory) so you can have different types/users.

If you need anymore help just let me know

PHP: Select all

<?php 
$idir 
"../images/gallery/full/";   // Path To Images Directory 
$tdir "../images/gallery/thumbs/";   // Path To Thumbnails Directory 
$twidth "100";   // Maximum Width For Thumbnail Images 
$theight "75";   // Maximum Height For Thumbnail Images 
$pic=($_FILES['imagefile']['name']);
$gallery $_POST['gallery'];
if (!isset(
$_POST['gallery'])) {
?> 
<fieldset>
    <legend><b>Image Gallery Upload</b></legend>
 <form enctype="multipart/form-data" action="<? $_SERVER['PHP_SELF']; ?>" method="post">  
     <p style="margin-left:10px;">Gallery to upload to:<br />
     <select name="gallery" size="1">
      <option value="jack">Jack</option> 
      <option value="honeylands">Honeylands</option> 
      <option value="events">Events</option> 
      <option value="art">Art Auction</option> 
     </select>
         <p style="margin-left:10px;">Image to upload:<br />
         <input type="file" name="imagefile" style="width:450px" /><br /> 
         <P><input type="submit" name="submit" value="Upload Image" class="submit-button" style="margin-left:10px;" /></p>
     </form>
</fieldset><br /> 
<?}
else {   
// Uploading/Resizing Script 
    
$url $_FILES['imagefile']['name'];   // Set $url To Equal The Filename For Later Use 
    
if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") { 
        
$file_ext strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php 
        
$copy copy($_FILES['imagefile']['tmp_name'], "$idir" $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location 
            
if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location 
         
print 'Image uploaded successfully.<br />';   // Was Able To Successfully Upload Image 
     
    //insert the image names into the database
    
include_once "../includes/connection.php";
    
$query "INSERT INTO image VALUES ('','$pic','$pic','$gallery')";
    
mysql_query($query);
    
mysql_close();
    
$simg imagecreatefromjpeg("$idir" $url);   // Make A New Temporary Image To Create The Thumbanil From 
    
$currwidth imagesx($simg);   // Current Image Width 
    
$currheight imagesy($simg);   // Current Image Height 
    
if ($currheight $currwidth) {   // If Height Is Greater Than Width 
        
$zoom $twidth $currheight;   // Length Ratio For Width 
        
$newheight $theight;   // Height Is Equal To Max Height 
        
$newwidth $currwidth $zoom;   // Creates The New Width 
    
} else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) 
        
$zoom $twidth $currwidth;   // Length Ratio For Height 
        
$newwidth $twidth;   // Width Is Equal To Max Width 
        
$newheight $currheight $zoom;   // Creates The New Height 
    

    
$dimg imagecreate($newwidth$newheight);   // Make New Image For Thumbnail 
    
imagetruecolortopalette($simgfalse256);   // Create New Color Pallete 
    
$palsize ImageColorsTotal($simg); 
    for (
$i 0$i $palsize$i++) {   // Counting Colors In The Image 
        
$colors ImageColorsForIndex($simg$i);   // Number Of Colors Used 
        
ImageColorAllocate($dimg$colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use 
    

    
imagecopyresized($dimg$simg0000$newwidth$newheight$currwidth$currheight);   // Copy Resized Image To The New Image (So We Can Save It) 
    
imagejpeg($dimg"$tdir" $url);   // Saving The Image 
    
imagedestroy($simg);   // Destroying The Temporary Image 
    
imagedestroy($dimg);   // Destroying The Other Temporary Image 
    
print 'Image thumbnail created successfully.';   // Resize successful 
    
} else { 
        print 
'<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed 
    


?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Oct 6th, 2007, 20:02
Highly Reputable Member
Join Date: Dec 2006
Location: Norwich
Posts: 721
Blog Entries: 4
Thanks: 6
Thanked 2 Times in 2 Posts
Re: Uploading

Quote:
Parse error: syntax error, unexpected $end in D:\Program Files\wamp\www\admin\test.php on line 68
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Oct 6th, 2007, 20:06
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Uploading

Missing a curly brace. Count each oif the curly braces and see which one is missing.

I had to cut down the script by a chunk beucase I have the script creating a thumbnail and another size image
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Oct 6th, 2007, 20:27
Highly Reputable Member
Join Date: Dec 2006
Location: Norwich
Posts: 721
Blog Entries: 4
Thanks: 6
Thanked 2 Times in 2 Posts
Re: Uploading

Were do i add one
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
<