Current location: Hot Scripts Forums » Programming Languages » PHP » Upload picture


Upload picture

Reply
  #1 (permalink)  
Old 08-13-04, 01:49 AM
skyloon skyloon is offline
New Member
 
Join Date: Aug 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Upload picture

I try to upload a picture & store the info about the picture
I faced some problem here, hope that u all can help me.....
the error message is "Can't open file!"
any codes need the be improved?
thanx...

If i use this code, i just can upload the file to the folder, not the info to the database....
PHP Code:

<?php

$uploaddir 
'./image/';
$uploadfile $uploaddir $_FILES['userfile']['name'];

print 
"<pre>";
if (
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   print 
"File is valid, and was successfully uploaded. ";
   print 
"Here's some more debugging info:\n";
   
print_r($_FILES);
} else {
   print 
"Possible file upload attack!  Here's some debugging info:\n";
   
print_r($_FILES);
}
print 
"</pre>";
?>

upload.php
PHP Code:

<body bgcolor="#FFFFFF" text="#000000">

<
form enctype="multipart/form-data" action="test.php" method="post">
 <
input type="hidden" name="MAX_FILE_SIZE" value="50000" />
 
Send this file: <input name="userfile" type="file" />
 <
input type="submit" value="Send File" />
</
form
test.php
PHP Code:

<?php

include "./common.inc";
// Max packet size
   
define("MAX_SQL",50000);
   
$filehandle fopen($tmp"rb") or die( "Can't open file!" ); 
   
$query=    "INSERT INTO files (name, type, size) VALUES(".
             
$DB->quote($name).", ".
             
$DB->quote($type).", ".
             
$DB->quote($size).
             
")";

   
// Execute Query
   
$result $DB->query($query);
   
$file_id mysql_insert_id();

// Copy the binary file data to the filedata table in sequential rows each containing MAX_SQL bytes
// Your table should have an index set to auto_increment
// Store the file_id to identify the data fragments
   
while (!feof ($filehandle)) { 
       
$data base64_encode(fread($filehandle,MAX_SQL)); 
       
$query "INSERT INTO filedata (file_id, data) VALUES($file_id,\"".$data."\")";
       
$result $DB->query($query); 
   }
   
fclose ($filehandle); 
?>

Decode the data fragments and recombine them:
<?php
   $file_id 
=$_GET ['file_id']; 
   
$query ="select file_id, name, type, size from files where file_id='$file_id'";
   
$result $DB->query($query);
   
$rowmysql_fetch_array ($result); 
   
$type $row ["type"]; 
   
$name $row ["name"]; 
   
$size $row ["size"]; 
   
$file_id $row ["file_id"]; 

   
// get the file data
   
$query "select id, data from filedata where file_id='$file_id' ORDER by id";
   
$result $DB->query($query);

// decode the fragments and recombine the file
   
$data "";
   while (
$row mysql_fetch_array($result)) {
       
$data .= base64_decode($row ["data"]);  
   }
   
// output the file
   
header ("Content-type: $type"); 
   
header ("Content-length: $size"); 
   
header ("Content-Disposition: attachment; filename=$name"); 
   
header ("Content-Description: PHP Generated Data"); 
   echo 
$data;
?>
common.inc
PHP Code:

<?php

$dbhost 
"localhost";
$dbusername "root";
$dbuserpassword "";
$default_dbname ="upload";

$MYSQL_ERRNO '';
$MYSQL_ERROR '';
function 
db_connect() {
global 
$dbhost$dbusername$dbuserpassword$default_dbname;
global 
$MYSQL_ERRNO$MYSQL_ERROR;
$link_id mysql_connect($dbhost$dbusername$dbuserpassword);
if(!
$link_id) {
$MYSQL_ERRNO 0;
$MYSQL_ERROR "Connection failed to the host $dbhost.";
return 
0;
}
else if(empty(
$dbname) && !mysql_select_db($default_dbname)) {
$MYSQL_ERRNO mysql_errno();
$MYSQL_ERROR mysql_error();
return 
0;
}
else return 
$link_id;
}
function 
sql_error() {
global 
$MYSQL_ERRNO$MYSQL_ERROR;
if(empty(
$MYSQL_ERROR)) {
$MYSQL_ERRNO mysql_errno();
$MYSQL_ERROR mysql_error();
}
return 
"$MYSQL_ERRNO$MYSQL_ERROR";
}
?>

database table

create table files(
name varchar(50),
type varchar(30),
size varchar(30));

create table filedata(
file_id int auto_increment primary key,
data varchar(30));
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
If statement in file upload mdhall PHP 4 04-12-04 05:33 PM
will pay. php script needed. upload, resize, & email jamjammo Script Requests 4 02-29-04 08:30 PM
simple picture upload script carlo_l Script Requests 3 01-08-04 01:16 PM
Help with my upload script TheMetsAreBad PHP 2 12-04-03 06:10 PM
Picture upload to database? dayzeday PHP 9 10-09-03 01:14 AM


All times are GMT -5. The time now is 04:19 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.