Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 19
  1. #1
    Senior Member
    Join Date
    Sep 2011
    Location
    South Africa
    Posts
    142
    Member #
    29386
    Hi all. I need some help with creating an image uploader & store the image path to mysql & the image in a directory instead of the db & then display the images on my portfolio page. Does anyone know of how to do this? Please help

    The script for uploader.php

    PHP Code:
    <?php

    // set variables
    $file 'file';
    $image_path 'image_path';
    $title 'title';
    $size 'size';
    $type 'type';
    $username "*****************";
    $password "***************";
    $localhost "**********";
    $database "****************";

    if(isset(
    $_POST['image_path']));
    $_SESSION['image_path'] = $_POST['image_path'];

    // connect to the database
    mysql_connect($localhost$username$password);
    mysql_select_db($database) or die( "Unable to select database");

    $query mysql_query("INSERT INTO images ('title','image_path','size','type') VALUES ('$title','$image_path','$size','$type')");
    mysql_close();
    ?>
    <!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>CA DESIGN - Uploader</title>
    <link href="admin_styles.css" rel="stylesheet" type="text/css" />
    <link rel="Shortcut Icon" href="favicon.ico">
    </style>
    <style type="text/css">
    a:link {
    color: #b9b9ba;
    text-decoration: none;
    }
    a:visited {
    text-decoration: none;
    color: #b9b9ba;
    }
    a:hover {
    text-decoration: none;
    color: #333;
    }
    a:active {
    text-decoration: none;
    }
    a {
    font-family: "AvantGarde Bk BT";
    font-weight: bold;
    }
    body,td,th {
    color: #272727;
    }
    </style>
    </head>

    <body><div id="container">
    <div id="wrapper">
    <?php require_once("includes/inc_header_container.php"); ?>
    <div id="content">
    <div id="uploader_1">
    <h4>Corporate Identity work</h4>
    <form action="../upload-file.php" method="POST" enctype="multipart/form-data" name="form1" id="form1">
    <label for="image_path"></label>
    <input type="file" name="file" id="file" />
    <input type="submit" name="upload" id="upload" value="Submit" />
    </form>
    </div>
    <div id="uploader_2">
    <h4>Print design</h4>
    </div>
    <div id="uploader_3">
    <h4>Web design</h4>
    </div>
    </div>
    <?php require_once("includes/inc_footer_container.php"); ?>
    </div><!-- Wrapper -->
    </div><!-- Container -->
    </body>
    </html>
    The upload-file.php script

    PHP Code:
    <?php
    // Start the session
    session_start();

    // Define variables
    $image_title "image_title";
    $image_path "image_path";
    $username "*********";
    $password "********";
    $localhost "***********";
    $database "***********";

    // Check if the value has been inserted
    if(isset($_POST['image_title']))
    $_SESSION['image_title'] = $_POST['image_title'];
    if(isset(
    $_POST['image_path']))
    $_SESSION['image_path'] = $_POST['image_path'];

    // Connect to database
    mysql_connect($localhost,$username,$password);
    mysql_select_db($database) or die( "Unable to select database");
    $uploaddir './uploads/';
    $file $uploaddir basename($_FILES['uploadfile']['name']);
    $max_file_size 20000000 ;
    if (
    move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
    echo 
    "success";

    // Create a query to insert data
    $query mysql_query ("INSERT INTO images ('image_title','image_path') VALUES ('".$_SESSION['image_title']."','".$_SESSION['image_path']."','$file')");
    } else {
    echo 
    "Uploading of files failed";
    }
    mysql_close();
    ?>

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,716
    Member #
    5580
    Liked
    718 times
    You have a lot of decisions to make here .....

    1) Can anyone upload, or just users that login?
    2) How many can they upload?
    3) What filetypes and Sizes?
    4) Does there have to be thumbnails created?
    5) Can people add/edit/remove images, or only upload?
    6) Should it be using AJAX (PHP and javascripting), or just a plain PHP uploader?

    Not enough info to answer your original post.


  4. #3
    Senior Member
    Join Date
    Sep 2011
    Location
    South Africa
    Posts
    142
    Member #
    29386
    Hi. This is a personal website. My own. So it will only be me doing uploading, updating & removing images.

    1. I want to have an uploader for each section of my portfolio page for instance Corporate identity , Print design & web design.
    2. Image type will be jpeg & png only
    3. No thumbnails in the admin section. I am using the javascript lightbox on my portfolio page.
    4. In the admin section where I can delete the posts I only want the name & path to the image to be displayed next to a delete button.
    4. Just a php uploader.
    5. Also store the path to the image in the database & the image in a directory (uploads)

  5. #4
    Senior Member
    Join Date
    Sep 2011
    Location
    South Africa
    Posts
    142
    Member #
    29386
    So I got to upload the image to the directory, but after the image upload is complete, it does not go to the index page I want it to go to. Can I have a link button coded into the upload-file.php script that will return me to the index page or how does this work? I see the image in the folder as well. I cant see any information of the file entry in the DB after it was uploaded.

    Question: The mysql INSERT query I have on the uploader.php file & on the upload-file.php file....Does that cause any conflict? Must I remove the INSERT query script from the uploader.php file?

    At the moment the code is working, with regards to the image being uploaded into the directory but I am just thinking will it rather be best to remove the INSERT query from the uploader.php file instead of having it on both(Doesn't make sense to have them on both if the code is working)?

    My script on the uploader.php page is like this:

    PHP Code:
    <?php
    session_start
    ();

    // set variables
    $file "file";
    $image_path "image_path";
    $title "title";
    $size "size";
    $type "type";
    $username "******";
    $password "************";
    $localhost "*********";
    $database "**********";

    // connect to the database
    mysql_connect($localhost$username$password);
    mysql_select_db($database) or die( "Unable to select database");

    $query mysql_query("INSERT INTO images ('title','image_path','size','type') VALUES ('$title','$image_path','$size','$type','$file')");
    mysql_close();
    ?>
    <!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>CA DESIGN - Uploader</title>
    <link href="admin_styles.css" rel="stylesheet" type="text/css" />
    <link rel="Shortcut Icon" href="favicon.ico">
    </style>
    <style type="text/css">
    a:link {
        color: #b9b9ba;
        text-decoration: none;
    }
    a:visited {
        text-decoration: none;
        color: #b9b9ba;
    }
    a:hover {
        text-decoration: none;
        color: #333;
    }
    a:active {
        text-decoration: none;
    }
    a {
        font-family: "AvantGarde Bk BT";
        font-weight: bold;
    }
    body,td,th {
        color: #272727;
    }
    </style>
    </head>

    <body><div id="container">
      <div id="wrapper">
        <?php require_once("includes/inc_header_container.php"); ?>
        <div id="content">
        <div id="uploader_1">
        <h4>Corporate Identity work</h4>
        <form action="../upload-file.php" method="POST" enctype="multipart/form-data" name="form1" id="form1">
          <input type="hidden" name="MAX_FILE_SIZE" value="20000000" />
          <input type="file" name="userfile" id="file" />
          <input type="submit" name="upload" id="upload" value="Submit" />
          <input type="reset" name="reset" id="reset" value="Reset" />
        </form>
        </div>
        <div id="uploader_2">
          <h4>Print design</h4>
        </div>
        <div id="uploader_3">
          <h4>Web design</h4>
        </div>
        </div>
      <?php require_once("includes/inc_footer_container.php"); ?>
      </div><!-- Wrapper -->
      </div><!-- Container -->
    </body>
    </html>
    On the upload-file.php script this is my code:

    PHP Code:
    <?php
    // Start the session
    session_start();

    // Define variables
    $title "title";
    $image_path "image_path";
    $size ="size";
    $type "type";
    $username "********";
    $password "********";
    $localhost "***********";
    $database "************";

    // Check if the value has been inserted
    //if(isset($_POST['title']))
    //$_SESSION['title'] = $_POST['title'];
    //if(isset($_POST['image_path']))
    //$_SESSION['image_path'] = $_POST['image_path'];

    // Connect to database
    mysql_connect($localhost,$username,$password);
    mysql_select_db($database) or die( "Unable to select database");
    $uploaddir './uploads/';
    $uploadfile $uploaddir basename($_FILES['userfile']['name']);
    $max_file_size 20000000 ;
    if (
    move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo 
    "File upload successful";

    // Create a query to insert data
    $query mysql_query ("INSERT INTO images ('title','image_path','size','type') VALUES ('$title','$image_path','$size','$type')");
    } else {
    echo 
    "Uploading of files failed";
    }
    mysql_close();
    ?>

  6. #5
    Senior Member
    Join Date
    Sep 2011
    Location
    South Africa
    Posts
    142
    Member #
    29386
    Hello, anything you can help me with here?

  7. #6
    WDF Staff AlphaMare's Avatar
    Join Date
    Oct 2009
    Location
    Montreal, Canada
    Posts
    4,570
    Member #
    20277
    Liked
    878 times
    Quote Originally Posted by Crispian, post: 239060
    Hello, anything you can help me with here?
    Be patient - less than 24 hours between your posts! This is a forum, not a chat room, and not everyone checks in on a daily basis.
    Good design should never say "Look at me!"
    It should say "Look at this." ~ David Craib


    http://digitalinsite.ca ~ my current site . . info@digitalinsite.ca ~ my email

    If you feel that someone's post helped you fix your problem, answered your question, or just made you feel better, feel free to "Like" their post. The "Like" link is at the bottom right of each post, along side the "reply" link. And if you are being helped here, try to help someone else - pass it on!

  8. #7
    Senior Member
    Join Date
    Sep 2011
    Location
    South Africa
    Posts
    142
    Member #
    29386
    Okay. Will keep that in mind

  9. #8
    Senior Member Noteleklabs's Avatar
    Join Date
    Jun 2012
    Location
    NC
    Posts
    217
    Member #
    31890
    Liked
    28 times
    I found this, if it helps any:
    http://www.developphp.com/view.php?tid=1139
    Hey you! Go make something!

  10. #9
    Senior Member
    Join Date
    Sep 2011
    Location
    South Africa
    Posts
    142
    Member #
    29386
    Thanks. Will have a look at this

  11. #10
    Senior Member medlington's Avatar
    Join Date
    Nov 2005
    Location
    Sheffield, UK
    Posts
    377
    Member #
    11968
    Liked
    4 times
    Hi,

    Are you still stuck with this?

    If you are heres a piece of one of my codes that uploads the image, then if its uploaded it resizes it and then saves the image location to a DB. The difference with mine is that it is updating an existing record in the DB rather than creating a new one. It also adds a random int onto the filename so that it doesnt overwite any files with the same name.

    Code:
    <?
    
    $target_path = "images/" . rand();
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    
    // rty resize
    
                            $src = imagecreatefromjpeg($target_path);
                            
                            // Capture the original size of the uploaded image
                            list($width,$height)=getimagesize($target_path);
                            
                            // For our purposes, I have resized the image to be
                            // 600 pixels wide, and maintain the original aspect
                            // ratio. This prevents the image from being "stretched"
                            // or "squashed". If you prefer some max width other than
                            // 600, simply change the $newwidth variable
                            $newwidth=277;
                            $newheight=($height/$width)*$newwidth;
                            $tmp=imagecreatetruecolor($newwidth,$newheight);
                            
                            // this line actually does the image resizing, copying from the original
                            // image into the $tmp image
                            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
                            
                            // now write the resized image to disk. I have assumed that you want the
                            // resized, uploaded image file to reside in the ./images subdirectory.
                            // $filename = "images/". $_FILES['uploadfile']['name'];
                            imagejpeg($tmp,$target_path,100);
                            
                            imagedestroy($src);
                            imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
    
    // uploaded so go back no probs and write to db
    
    // write the image path to the DB
    include 'sql/config.php';
    include 'sql/opendb.php';
    
    $result = mysql_query("UPDATE carly_teacher
                            SET image='$target_path'
                            WHERE id='$id'")
                            or die(mysql_error()); 
    
    
    } else{
    // not upladed but details have been added so go back with message to edit
    
    
    }
    include 'sql/closedb.php';
    ?>
    Hope that helps


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:00 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com