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