Jump to content

[SOLVED] help with image upload please


meomike2000

Recommended Posts

i have been trying to create an image upload so that users can upload an image and then store that image in the datebase.

 

i have read many how to s on this and i am still having trouble.

this is the code that i got.

 

$uploaddir = "images"; 
if(is_uploaded_file($_FILES['file']['tmp_name']))

{

move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);



echo "Your file has been uploaded successfully! Yay!";

  }
else
  {
  $img = "Invalid file";
  }

 

the end result for me is always invalid file.

 

i am a little confused on the basedir. is that starting at the dir. that the script is in or what. sorry to be so confused. i have been at this for hours and am having no luke.

i would like the end result to be that the image is now stored in $img. so that it can be input into the update query. thanks for any hlep......

 

mike

Link to comment
https://forums.phpfreaks.com/topic/146996-solved-help-with-image-upload-please/
Share on other sites

From memory, the script should look more like the following:

$uploaddir = "images"; 
if(move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']))
  {
  echo "Your file has been uploaded successfully! Yay!";
  } else {
  $img = "Invalid file";
  }

tried that way aswell,  still no luck, starting to think that it is a permissions issue.

 

not real sure on the $uploaddir = "images";  folder, i mean were would this folder be, i assume that it would be in the same directory as the script. is that wrong......

 

please i really need some help here.......

 

thanks in advance mike.

have changed the code to :

 

$uploaddir = "images";

if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$_FILES['imagefilefile']['name']))

{

 

 

echo "Your file has been uploaded successfully! Yay!";

 

  }

else

  {

  $img = "Invalid file";

  }

 

 

now i get this error:

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpuG1bTf' to '' in /home/mike/web/home/editit.php on line 12

 

help please.......

when set this way:

 

$uploaddir = "images"; 
//if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$uploaddir $_FILES['imagefilefile']['name']))
//{
if (move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img"))
{
        $instr = fopen("latest.img","rb");
        $image = addslashes(fread($instr,filesize("latest.img")));

echo "Your file has been uploaded successfully! Yay!";

  }
else
  {
  $img = "Invalid file";
  }

i get this:

Warning: move_uploaded_file(latest.img) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/mike/web/home/editit.php on line 14

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php1gY3lh' to 'latest.img' in /home/mike/web/home/editit.php on line 14

ok i have worked out some of the problem...

 

changed code to

$uploaddir = "images"; 
//if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$uploaddir $_FILES['imagefilefile']['name']))
//{
if (move_uploaded_file($_FILES['imagefile']['tmp_name'],"images/latest.img"))
{
        $instr = fopen("images/latest.img","rb");
        $image = addslashes(fread($instr,filesize("images/latest.img")));

$img = $image;

  }
else
  {
  $img = null;
  }

 

and the file uploads and is stored in the database.....

 

problem now is when i try to view the image, all i get is encrypted data on the screen....

please please help...... mike

ok this is what i got and it works somewhat.

 

<?php
$target = "images/";
$target = $target . basename( $_FILES['imagefile']['name']); 
$img = ($_FILES['imagefile']['name']);

//then i update the database.

//then i

move_uploaded_file($_FILES['imagefile']['tmp_name'], $target);

//then script carries on.
?>

 

can anybody help me set this so that only images get uploaded, and preferrable less than 1mb or less if there is a way to make the photos require less.......

 

thanks in advance mike.....

You can use this solution as reference:

 

upload.php

<html>
<head>
<title>Upload File To MySQL Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.box {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
border: 1px solid #000000;
}
-->
</style>
</head>

<body>
<?PHP
$db_link=mysqli_connect("localhost", "root", "") or die(mysqli_error($db_link));
mysqli_select_db($db_link,"plaincart") or die(mysqli_error($db_link));
if(isset($_POST['upload']))
{
	$fileName = $_FILES['userfile']['name'];
	$tmpName  = $_FILES['userfile']['tmp_name'];
	$fileSize = $_FILES['userfile']['size'];
	$fileType = $_FILES['userfile']['type'];
	//move_uploaded_file($tmpName, '/uploads/test/'.$fileName) or die('error'); 

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

	if(!get_magic_quotes_gpc())
	{
		$fileName = addslashes($fileName);
	}



	$query = "INSERT INTO upload (name, size, type, content ) ".
	         "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

	mysqli_query($db_link,$query) or die('Error, query failed');					


	echo "<br>File $fileName uploaded<br>";
	//echo $tmpName;
}		
?>

<form action="" method="post" enctype="multipart/form-data" name="uploadform">
  <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr> 
      <td width="246"><input name="userfile" type="file" class="box" id="userfile">
	 </td>
      <td width="80"><input name="upload" type="submit" class="box" id="upload" value="  Upload  "></td>
    </tr>
  </table>
</form>
</body>
</html>

 

download.php

<?PHP
$db_link=mysqli_connect("localhost", "root", "") or die(mysqli_error($db_link));
mysqli_select_db($db_link,"plaincart") or die(mysqli_error($db_link));
if(isset($_GET['id']))
{


$id      = $_GET['id'];
$query   = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
$result  = mysqli_query($db_link,$query) or die('Error, query failed');
list($name, $type, $size, $content) = mysqli_fetch_array($result);

header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;


exit;
}

?>

<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?PHP
//This section lists the available files for dowload in database.
$db_link=mysqli_connect("localhost", "root", "") or die(mysqli_error($db_link));
mysqli_select_db($db_link,"plaincart") or die(mysqli_error($db_link));

$query  = "SELECT id, name FROM upload";
$result = mysqli_query($db_link,$query) or die('Error, query failed');
if(mysqli_num_rows($result) == 0)
{
echo "Database is empty <br>";
} 
else
{
while(list($id,$name) = mysqli_fetch_array($result))
{
?>
<a href="download.php?id=<?PHP echo $id;?>"><?PHP echo $name;?> </a> <br>
    
<?PHP	
}
}
?>
</body>
</html>

 

MySQL upload table definition:

CREATE TABLE upload (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
content BLOB NOT NULL,
PRIMARY KEY(id)
);

 

Hope you find this helpful.

 

Angel

that stores them well but how do i view them now.

 

i tried this style... all i get is encrypted data again.....

 


$query  = "SELECT content, phtag, phname FROM uphotos where fk_uid = '" . $userid . "'";
$resultph = mysqli_query($query) or die('Error, query failed');

if(mysql_num_rows($resultph) == 0)
{
   echo "upload some photos <br>";
}
else
{
while ($row = mysql_fetch_row($resultph))
   {
    echo '<img src="'.$row[0].'"><br>' . $row[1] . ' &nbsp ' . $row[3] . '<br><br><br>';   
   }
}

 

like i said this is my first try at image uploads......

yes that is what i want, image in database not as a file in directory. i set it up off what you showed me, not sure i got it right yet. all i get is encrypted data on the screen when i try to view them on the screen.

 

any ideas on that.

 

 

well here is what i got...

 

it works, uploads photos, stores them in a directory on disk, wish i didnt have to save them to the disk, stores the information about the photo in database.

 

//open database
//retrieve form data. 
$tag = $_POST['tag'];
$target = "images/";
$target = $target . basename( $_FILES['imagefile']['name']); 
//$img = ($_FILES['imagefile']['name']);
$img = $target;

if (move_uploaded_file($_FILES['imagefile']['tmp_name'], $target))
//}
$filesize = ($_FILES['imagefile']['size']);
$filename = ($_FILES['imagefile']['name']);
$filetype = ($_FILES['imagefile']['type']);

if ($filetype = 'image/jpeg')
{
if ($filesize < 1000000)
{
           //here i query the database and insert info.. then close connection, return to     upload page.... done....
         }
}

 

i can call the photos up to view like this

 

//open database and query
// process results here
if(mysql_num_rows($resultimg) == 0)
{
   echo "upload some photos <br>";
}
else
{
	while ($row = mysql_fetch_row($resultimg))
  {
  	echo "<br> filename='".$row[2]."'<br> ";
   	        echo "Content-tag: '".$row[1]."'<br>";
   	        echo "Content-length: '".$row[3]."'<br>";
   	        echo '<img class="images" src="/the path to photos/'.$row[0].'"  
                width=75px  height=75px><br>';
         }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.