Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-09-2011, 03:23 PM   PM User | #1
coolcool
New Coder

 
Join Date: Nov 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
coolcool is an unknown quantity at this point
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
coolcool is offline   Reply With Quote
Old 11-09-2011, 06:11 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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 06:18 PM..
mlseim is offline   Reply With Quote
Old 11-10-2011, 01:46 PM   PM User | #3
coolcool
New Coder

 
Join Date: Nov 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
coolcool is an unknown quantity at this point
thanks for your reply

Now i am getting below errors

Quote:
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
coolcool is offline   Reply With Quote
Old 11-10-2011, 02:07 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
redisplay the script .. I don't know where line 42 is.
mlseim is offline   Reply With Quote
Old 11-10-2011, 02:27 PM   PM User | #5
coolcool
New Coder

 
Join Date: Nov 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
coolcool is an unknown quantity at this point
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>
"
;
?>
coolcool is offline   Reply With Quote
Old 11-10-2011, 04:21 PM   PM User | #6
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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);
mlseim is offline   Reply With Quote
Old 11-11-2011, 05:29 AM   PM User | #7
coolcool
New Coder

 
Join Date: Nov 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
coolcool is an unknown quantity at this point
file_tmp: C:\wamp\tmp\php6A77.tmp
location: upload/78/C:\wamp\tmp\php6A77.tmp
coolcool is offline   Reply With Quote
Old 11-11-2011, 12:27 PM   PM User | #8
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:32 PM.


Advertisement
Log in to turn off these ads.