Current location: Hot Scripts Forums » Programming Languages » PHP » Image Uploading


Image Uploading

Reply
  #1 (permalink)  
Old 06-03-09, 01:52 PM
SniperTowers SniperTowers is offline
Wannabe Coder
 
Join Date: Dec 2008
Posts: 143
Thanks: 5
Thanked 0 Times in 0 Posts
Image Uploading

PHP Code:

<?php

session_start
();
// Admin Section
require ("connect.php");
require(
"functions.php"); 
include(
"template/header.php");
if(
$_SESSION['status'] != 2){
    die(
'You must be an administrator to access this section; please <a href="login.php">login</a>');
}
echo 
"<a href=\"admin.php\">Return to admin section!</a></br><br/><br/>";

echo 
"Here you can add layouts. If you wish to add a category, please go to <a href=\"admin-cats.php\">this</a> section!<br/><br/>";


echo 
"Create a new layout:<br/><br/>";
if(!isset(
$_POST['submit'])){
        echo 
"<form action=\"\" method=\"post\" enctype=\"multipart/form-data\">
        Title: <input type=\"text\" name=\"title\"><br/>
        Link to the layout: <input type=\"text\" name=\"link\"><br/>
Display image (100px x 100px): <input id=\"file\" type=\"file\" name=\"file\"/><br />

        "
;


        
listcats();
        echo 
"<br/><input type=\"submit\" name=\"submit\" value=\"Create layout!\">
        </form>"
;
        }else{
      
$rand rand(0,999999);
      
move_uploaded_file($_FILES["file"]["tmp_name"], "images/" "$rand$_FILES["file"]["name"]);
      
$image "$rand$_FILES["file"]["name"];
      if(empty(
$_FILES["file"]["name"])){
          
$image "none.jpeg";
}



        
$title mysql_real_escape_string($_POST['title']);
        
$link mysql_real_escape_string($_POST['link']);
        
$cat mysql_real_escape_string($_POST['cats']);
        if(empty(
$title)|| empty($link) || empty($cat)){
        die (
'You have not filled in all the fields required!');

        }
        
mysql_query("INSERT into layouts VALUES(NULL, '$title', '$link', '$image', '0', '$cat')");
        echo 
"The layout has been created!";
        echo 
"$image";
        }
        include(
"template/footer.php");
?>
Here's my php code. It currently adds the name of a layouts, the link and image. How do I upload another image in the same file to a different row in a database?
Reply With Quote
  #2 (permalink)  
Old 06-04-09, 10:14 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Your logic is a little quirky.
The reason I say this is because when all the required fields have not been filled in, then the form is no longer available to make corrections.
I rewrote your code so that you can make corrections until all the required fields have been filled in.
I also enclosed the form fields in a table for a more appealing display.
I don't know what listcats() output looks like so I didn't know exactly how to present it in the table.
PHP Code:

<?php
session_start
();
// Admin Section
if($_SESSION['status'] != 2){die('You must be an administrator to access this section; please <a href="login.php">login</a>');}
require (
"connect.php");
require(
"functions.php");
include(
"template/header.php");
$errorMsg "";
if(!empty(
$_POST['submit']))
{
 
$title mysql_real_escape_string($_POST['title']);
 
$link mysql_real_escape_string($_POST['link']);
 
$cat mysql_real_escape_string($_POST['cats']);
 if(empty(
$title)|| empty($link) || empty($cat)){$errorMsg " --- <b><u>All required(*) fields must be filled in!</u></b>";}
 else
 {
  
$rand rand(0,999999);
  
$image = empty($_FILES["file"]["name"]) ? "none.jpeg" "$rand$_FILES["file"]["name"];
  if(
$image != "none.jpeg"){move_uploaded_file($_FILES["file"]["tmp_name"], $image);}
  
mysql_query("INSERT into layouts VALUES(NULL, '$title', '$link', '$image', '0', '$cat')") or die(mysql_error());
  echo 
"The layout has been created! - $image";
  die();
  }
 }
echo 
"<a href=\"admin.php\">Return to admin section!</a></br><br/><br/>
      Here you can add layouts. If you wish to add a category, please go to <a href='admin-cats.php'>this</a> section!<br/><br/>
      Create a new layout:<span style='color:#f00;'>
$errorMsg</span>
      <form action='#' method='post' enctype='multipart/form-data'>
       <table>
        <tr><td align='right'>Title: </td><td><input type='text' name='title' value='"
.$_POST["title"]."'> <span style='color:#f00;font-weight:bold;'>*</span></td></tr>
        <tr><td align='right'>Link to the layout: </td><td><input type='text' name='link' value='"
.$_POST["link"]."'> <span style='color:#f00;font-weight:bold;'>*</span></td></tr>
        <tr><td>Display image (100px x 100px): </td><td><input id='file' type='file' name='file'/></td></tr>
        <tr><td colspan='2'>"
;listcats();echo "</td></tr>
        <tr><td colspan='2' align='center'><br /><input type='submit' name='submit' value='Create layout!'></td></tr>
       </table>
      </form>"
;
include(
"template/footer.php");
?>
Could you please explain exactly what this means?
Quote:
How do I upload another image in the same file to a different row in a database?
__________________
Jerry Broughton

Last edited by job0107; 06-04-09 at 10:34 AM.
Reply With Quote
  #3 (permalink)  
Old 06-04-09, 10:25 AM
SniperTowers SniperTowers is offline
Wannabe Coder
 
Join Date: Dec 2008
Posts: 143
Thanks: 5
Thanked 0 Times in 0 Posts
Thanks. Sorry I mean, the script uploads a file and puts the filename into the database into a field called image. I would like it so I can upload another image and that filename goes into another field called thumbnail.
Reply With Quote
  #4 (permalink)  
Old 06-04-09, 01:18 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Just add another input element (type file).
__________________
Jerry Broughton
Reply With Quote
  #5 (permalink)  
Old 06-04-09, 02:18 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Or resize the original image into a thumbnail - using ImageMagick or PHP.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
div css theighost CSS 11 09-14-08 02:30 AM
Image Not Uploading tommyc325 PHP 22 10-24-06 08:58 AM
Image uploading help Dan Man PHP 4 09-28-06 03:44 AM
[Programmer Needed] Customer Image Uploading Script *** xxkylexx Job Offers & Assistance 5 06-03-06 07:02 PM
Multiple Image Uploading php-learner PHP 2 01-17-05 05:46 PM


All times are GMT -5. The time now is 08:37 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.