...

View Full Version : File upload help



coolcool
11-09-2011, 03:23 PM
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
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
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

mlseim
11-09-2011, 06:11 PM
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);



.

coolcool
11-10-2011, 01:46 PM
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

mlseim
11-10-2011, 02:07 PM
redisplay the script .. I don't know where line 42 is.

coolcool
11-10-2011, 02:27 PM
thanks
line 42
move_uploaded_file($file_tmp, $location);
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>
";
?>

mlseim
11-10-2011, 04:21 PM
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);

coolcool
11-11-2011, 05:29 AM
file_tmp: C:\wamp\tmp\php6A77.tmp
location: upload/78/C:\wamp\tmp\php6A77.tmp

mlseim
11-11-2011, 12:27 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum