Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question File upload help

    database table name:uploadtest
    2 columns:id,imagelocation (id-> auto inc and primary)

    Need
    when user submit form(i.e: upload image)
    image need to store in upload/id/imagename (id-> auto inc and primary)

    and write the pathe name (i.e:upload/id/imagename) in imagelocation column

    Example: when user upload the image cool.jpg first time, store the image in 'upload/1/cool.jpg'
    and in imagelocation column the value is upload/1/cool.jpg and id value is 1.

    2nd time,store the image in 'upload/2/cool.jpg'
    and in imagelocation column the value is upload/2/cool.jpg and id value is 2

    my code
    PHP Code:
    <?php
    include '../include/functions.php';
    include 
    'block.php';
    if(!
    loggedin()) {
        
    header("Location: login.php");
        exit ();
    }

    if(isset(
    $_POST['submit']))
    $submit trim($_POST['submit']);


    if (isset (
    $submit)) {
        
    $errors = array();
        
    $allowed_ext = array('jpg','jpeg','png');
       
        
    $file_name $_FILES['image']['name'];
        
    $file_ext strtolower(end(explode('.'$file_name)));
        
    $file_size $_FILES['image']['size'];
        
    $file_tmp $_FILES['image']['tmp_name'];
       
           
       
       
        if(
    in_array($file_ext$allowed_ext) === false) {
            
    $errors[] = 'Extension not allowed';
        }
       
        if(
    $file_size 2097152) {
            
    $errors[] = 'File size must be under 2MB';
        }
       
        if(empty (
    $errors)) {
           
           
           
            
    $query mysql_query("INSERT INTO uploadtest VALUES ('','$location')");
            
    mkdir('upload/'.mysql_insert_id(), 0744);
            
    $id mysql_insert_id();
            
    $location "upload/$id/$file_name";
           
            
    move_uploaded_file($file_tmp$location);
           
           
           
        } else {
            foreach (
    $errors as $error) {
                echo 
    $error'<br />';
            }
        }
    }
    echo 
    "
        Upload Your image:
        <form action='upload2.php' method='POST' enctype='multipart/form-data'>
        <p>
            <input type='file' name='image'></input>
            <input type='submit' name='submit' value='upload'></input>
        </p>
       </form>
    "
    ;
    ?>
    The problem is, it creates folder based on the id and stores the image. but the uploaded file location field in the database is empty.
    plz help me friends

    And also tried this
    PHP Code:
    <?php
    include '../include/functions.php';
    include 
    'block.php';
    if(!
    loggedin()) {
        
    header("Location: login.php");
        exit ();
    }

    if(isset(
    $_POST['submit']))
    $submit trim($_POST['submit']);


    if (isset (
    $submit)) {
        
    $errors = array();
        
    $allowed_ext = array('jpg','jpeg','png');
       
        
    $file_name $_FILES['image']['name'];
        
    $file_ext strtolower(end(explode('.'$file_name)));
        
    $file_size $_FILES['image']['size'];
        
    $file_tmp $_FILES['image']['tmp_name'];
       
           
       
       
        if(
    in_array($file_ext$allowed_ext) === false) {
            
    $errors[] = 'Extension not allowed';
        }
       
        if(
    $file_size 2097152) {
            
    $errors[] = 'File size must be under 2MB';
        }
       
        if(empty (
    $errors)) {
           
           
            
    $location "upload/$id/$file_name";
            
    $query mysql_query("INSERT INTO uploadtest VALUES ('','$location')");
            
    mkdir('upload/'.mysql_insert_id(), 0744);
            
    $id mysql_insert_id();
            
    move_uploaded_file($file_tmp$location);
           
           
           
           
        } else {
            foreach (
    $errors as $error) {
                echo 
    $error'<br />';
            }
        }
    }
    echo 
    "
        Upload Your image:
        <form action='upload2.php' method='POST' enctype='multipart/form-data'>
        <p>
            <input type='file' name='image'></input>
            <input type='submit' name='submit' value='upload'></input>
        </p>
       </form>
    "
    ;
    ?>
    but it it creates the folder based on id but the image stores inside the upload folder. Example: id=78 and imagelocation=upload//scary-web-design.png

    Plz help friends thanks

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    You won't know the id until you insert a row, and you won't know
    the path until you've already inserted the row. So, I'm thinking you
    need to create the row with a blank, and then update it after you
    create the filename ... putting the horse after the cart, not before.


    In your 2nd script ... this section:

    $location = "upload/$id/$file_name";
    $query = mysql_query("INSERT INTO uploadtest VALUES ('','$location')");
    mkdir('upload/'.mysql_insert_id(), 0744);
    $id = mysql_insert_id();
    move_uploaded_file($file_tmp, $location);


    Change to this:

    $location = "blank";
    $query = mysql_query("INSERT INTO uploadtest (imagelocation) VALUES ('$location')");
    $id = mysql_insert_id();
    $location = "upload/$id/$file_tmp";
    $query = mysql_query("UPDATE uploadtest SET imagelocation = '$location' WHERE id = '$id'");
    $path = "upload/$id/";
    mkdir('$path', 0744);
    move_uploaded_file($file_tmp, $location);



    .
    Last edited by mlseim; 11-09-2011 at 07:18 PM.

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks for your reply

    Now i am getting below errors

    Warning: move_uploaded_file(upload/76/C:\wamp\tmp\phpE715.tmp) [function.move-uploaded-file]: failed to open stream: Invalid argument in C:\wamp\www\swdadmin\upload2.php on line 42

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\phpE715.tmp' to 'upload/76/C:\wamp\tmp\phpE715.tmp' in C:\wamp\www\swdadmin\upload2.php on line 42
    and it just creates '$path' folder inside my root and in my imagelocation field the value is 'upload/74/C:wamp mpphp8CE3.tmp'

    any other suggestions? thanks

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    redisplay the script .. I don't know where line 42 is.

  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks
    line 42
    PHP Code:
    move_uploaded_file($file_tmp$location); 
    code
    PHP Code:
    <?php
    include '../include/functions.php';
    include 
    'block.php';
    if(!
    loggedin()) {
        
    header("Location: login.php");
        exit ();
    }

    if(isset(
    $_POST['submit']))
    $submit trim($_POST['submit']);


    if (isset (
    $submit)) {
        
    $errors = array();
        
    $allowed_ext = array('jpg','jpeg','png');
        
        
    $file_name $_FILES['image']['name'];
        
    $file_ext strtolower(end(explode('.'$file_name)));
        
    $file_size $_FILES['image']['size'];
        
    $file_tmp $_FILES['image']['tmp_name'];
        
            
        
        
        if(
    in_array($file_ext$allowed_ext) === false) {
            
    $errors[] = 'Extension not allowed';
        }
        
        if(
    $file_size 2097152) {
            
    $errors[] = 'File size must be under 2MB';
        }
        
        if(empty (
    $errors)) {
            
            
    $location "blank";
            
    $query mysql_query("INSERT INTO uploadtest (imagelocation) VALUES ('$location')");
            
    $id mysql_insert_id();
            
    $location "upload/$id/$file_tmp";
            
    $query mysql_query("UPDATE uploadtest SET imagelocation = '$location' WHERE id = '$id'");
            
    $path "upload/$id/";
            
    mkdir('$path'0744);
            
    move_uploaded_file($file_tmp$location);
            
            
            
            
        } else {
            foreach (
    $errors as $error) {
                echo 
    $error'<br />';
            }
        }
    }
    echo 
    "
        Upload Your image:
        <form action='upload2.php' method='POST' enctype='multipart/form-data'>
        <p>
            <input type='file' name='image'></input>
            <input type='submit' name='submit' value='upload'></input>
        </p>
       </form>
    "
    ;
    ?>

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    Let's see what values are by echoing them ... just to test.
    Add the part in red so we can see what those variables are.
    That might help us solve it.


    $location = "blank";
    $query = mysql_query("INSERT INTO uploadtest (imagelocation) VALUES ('$location')");
    $id = mysql_insert_id();
    $location = "upload/$id/$file_tmp";
    $query = mysql_query("UPDATE uploadtest SET imagelocation = '$location' WHERE id = '$id'");

    echo "file_tmp: $file_tmp<br>";
    echo "location: $location";
    exit;


    $path = "upload/$id/";
    mkdir('$path', 0744);
    move_uploaded_file($file_tmp, $location);

  • #7
    New Coder
    Join Date
    Nov 2011
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    file_tmp: C:\wamp\tmp\php6A77.tmp
    location: upload/78/C:\wamp\tmp\php6A77.tmp

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    You're using a local server?
    I think I have the slashes going the wrong way, because I'm on a shared server.

    And of course, the path is all wrong anyhow. C:\wamp\tmp is not what
    you want. You're looking for the filename, not the temp name.
    I guess I don't know what to change now.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •