Jump to content

upload a picture


mydownfall00

Recommended Posts

okay, so this may be a dumb question but i looked through previous threads and couldn't find the answer so this is why i am asking..

 

the other day i got my sign up form to work, now since i have it working i want to expand it and add more for the user so I want them to be able to upload a picture, however I am in the process of making the table and im not exactly sure what I should set the type for the field picture.. does it have to be a varchar and it's all done in the script? any help would be appreciated.

Link to comment
Share on other sites

You cant use varchar, you should use BLOB...

 

Here is an upload to database script I use, you can use for reference...

 

$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$File = preg_replace("/[&'_ ]/","",$fileName); // strip whitespace and special characters
$data = addslashes(fread(fopen($File, "r"), filesize($File)));

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
$ClientID = $_POST['clientid'];
$description = $_POST['description'];
$query = "INSERT INTO upload (name, size, type, content, ClientID, description) ".
"VALUES ('$File', '$fileSize', '$fileType', '$content', '$ClientID', '$description')";

$result = mysql_query($query) or die(mysql_error()); 

 

then just use a form with a file and feild and point it to something similar... there are plenty of tutorials out there using google. Then you could use this to download the file...

  $sql = "SELECT content, type, name, size FROM upload WHERE id=$id";

  $result = @mysql_query($sql);
  $data = @mysql_result($result, 0, "content");
  $name = @mysql_result($result, 0, "name");
  $size = @mysql_result($result, 0, "size");
  $type = @mysql_result($result, 0, "type");

  header("Content-type: $type");
  header("Content-length: $size");
  header("Content-Disposition: attachment; filename=$name");
  header("Content-Description: PHP Generated Data");
  echo $data;

 

 

Link to comment
Share on other sites

As for the database, you should store them as BLOB if you are going to actually store the image in there.

 

You wouldnt normally store the actual image in the database, rather store/fetch its name from the database and include that in a html <img src="images/<?=$image_name_here?>"> tag..

 

General speaking the files will be created 0644 but if that is not the case on your server you will need to chmod() them to at least this permission for them to be web viewable.

Link to comment
Share on other sites

for the script you gave me to upload a file, is that just the script just for the upload file..?

 

reading through the script, it seems that $file is the field for the file, in my table I named it $picture, so I would go through your code and replace the two...??? also for the query (insert into), that's going to add the rest of the values into the table correct?

Link to comment
Share on other sites

Right, so my table has these fields...

name, size, type, content, ClientID, description

but just the content one needs to be BLOB, the rest can be varchar...

Then as another example, which is not the best since I like to escape out of PHP to write HTML, but this lists the files in the database and points to download.php which will auto download the file.

  <?php
$sql = "SELECT * FROM upload WHERE ClientID = 'Whateveritis'";
$sql .= "ORDER BY name ASC";
$result = mysql_query($sql) or die(mysql_error());

$rows = mysql_num_rows($result);

for ($i = 0; $i < $rows; $i++) {
  $data = mysql_fetch_object($result);
?>
    <tr>
      <td width="260" scope="col"><a href='download.php?id=<?php echo $data->id; ?>'><?php echo $data->name; ?></a></td>
      <td width="330" scope="col"><?php echo stripslashes($data->description); ?></td>
    </tr>
  
  <?php
}
?>

Link to comment
Share on other sites

okay i gave it a shot with your code and I have some errors. I probably have this all wrong.. xD here are my errors

 

Warning: fread(): supplied argument is not a valid stream resource in /home/shopceme/public_html/mydownfall/add2.php on line 46

 

Warning: fread(): supplied argument is not a valid stream resource in /home/shopceme/public_html/mydownfall/add2.php on line 49

 

Warning: fclose(): supplied argument is not a valid stream resource in /home/shopceme/public_html/mydownfall/add2.php on line 51

Error with query: Duplicate entry '0' for key 1

 

and here is my add2.php code.. above this is my connection which i know is working.. and the rest is how I have my table setup.. except i have an id at th beginning auto_increment..

 

$username=$_POST['username'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname']; 
$email=$_POST['email']; 
$age=$_POST['age']; 
$picture=$_POST['picture']; 
$band=$_POST['band']; 
$description=$_POST['description']; 


$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$File = preg_replace("/[&'_ ]/","",$fileName); // strip whitespace and special characters
LINE 46  $data = addslashes(fread(fopen($File, "r"), filesize($File)));

$fp      = fopen($tmpName, 'r');
LINE 49   $content = fread($fp, filesize($tmpName));
$content = addslashes($content);
LINE 51fclose($fp);
$ClientID = $_POST['clientid'];
$description = $_POST['description'];
$query = "INSERT INTO users(username, firstname, lastname, email, age, picture, band, description) ".
"VALUES ('$username', '$firstname', '$lastname', '$email', '$age', '$picture', 'band', 'description')";

$result = mysql_query($query) or die(mysql_error()); 


$result = mysql_query($query); 
if (!$result) { 
    $errormessage = mysql_error(); 
    echo "Error with query: " . $errormessage; 
    exit(); 
} 
printf ("<center>These values were inserted into the database<BR><BR>

<table border='0'>
<tr>
<td>
username
</td>
<td>%s
</td>
</tr>
<tr>
<td>
first name
</td>
<td>%s
</td>
</tr>
<tr>
<td>
last name
</td>
<td>%s
</td>
</tr>
</table>", $username, $firstname, $lastname);          
?>

 

 

 

 

 

 

$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

 

this is what im confused by, are you assinging these in the script??

Link to comment
Share on other sites

Okay, just change userfile to picture like below...

and you dont use POST to grab files, you use $_FILES. Here is your revised code...

$username=$_POST['username'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname']; 
$email=$_POST['email']; 
$age=$_POST['age']; 
$band=$_POST['band']; 
$description=$_POST['description']; 


$fileName = $_FILES['picture']['name'];
$tmpName  = $_FILES['picture']['tmp_name'];
$fileSize = $_FILES['picture']['size'];
$fileType = $_FILES['picture']['type'];

$File = preg_replace("/[&'_ ]/","",$fileName); // strip whitespace and special characters
$data = addslashes(fread(fopen($File, "r"), filesize($File)));

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

$query = "INSERT INTO users(username, firstname, lastname, email, age, picture, band, description) ".
"VALUES ('$username', '$firstname', '$lastname', '$email', '$age', '$content', '$band', '$description')";

$result = mysql_query($query) or die(mysql_error()); 

this is untested but try it out and see what happens

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.