Page 1 of 1

uploading file.... problem.... need help

Posted: Mon Feb 20, 2012 2:00 am
by makuletz27
i want to know if the file that i save in the database is the original file...

because when i call the file it only give me the path were i save the file....

the file that i want to upload is jpeg file....

Code: Select all

<?php
include 'db-connect.php';

if($_SERVER["REQUEST_METHOD"]=="POST"){
	$name = $_FILES['file']['name'];
	
	$extension = strtolower(substr($name, strpos($name, '.')+1));
	$tym_name = $_FILES['file']['tmp_name'];
	$type = $_FILES['file']['type'];
	$size = $_FILES['file']['size'];
	$max_size =  2097152;
	
	if(isset($name)){
		if(!empty($name)){
			if(($extension=='jpg'||$extension=='jpeg')&& $type=='image/jpeg' && $size<=$max_size){
				
				$location = 'upload/upload_files';
				if(move_uploaded_file($tym_name, $location."/".$name)){
					
				
					$sql ="INSERT INTO profile_photo (id,filename,size)"."VALUES('".$_SESSION['id']."','$tym_name','$size')";
					mysql_query($sql) or die('Error: ' . mysql_error());
					echo "1 record added";						
										
				}	

			}
			else{
				echo "the file must be jpg/jpeg and must be less than 2mb ";
			}
		}else{
			echo "Please Choose a file. ";
		}	
	}	
}
?>

<html>
	<body>
		<form method="post" enctype="multipart/form-data">
			<input type="file" name="file"/><br>
			<input type="submit" name="submit"/>
		</form>

	</body>
</html>

Re: uploading file.... problem.... need help

Posted: Mon Feb 20, 2012 3:04 am
by social_experiment

Code: Select all

$tym_name = $_FILES['file']['tmp_name'];
This contains the path to the temporary file; try using this

Code: Select all

$tym_name = $_FILES['file']['name'];
hth

Re: uploading file.... problem.... need help

Posted: Mon Feb 20, 2012 7:43 am
by makuletz27
when i try to change the

Code: Select all

$tym_name = $_FILES['file']['tmp_name'];
to

Code: Select all

$tym_name = $_FILES['file']['name'];
it didn't work...

it aggravated the code....

Re: uploading file.... problem.... need help

Posted: Mon Feb 20, 2012 9:08 am
by social_experiment
makuletz27 wrote:it aggravated the code....
how

Re: uploading file.... problem.... need help

Posted: Mon Feb 20, 2012 9:18 am
by makuletz27
nothing happen when i change the
the file didn't save

Code: Select all

$tym_name = $_FILES['file']['tmp_name'];
to

Code: Select all

$tym_name = $_FILES['file']['name'];
when im using
$tym_name = $_FILES['file']['tmp_name'];
it save at the database and to the

Code: Select all

$location = 'upload/upload_files';
but it only save the name of the file that i uploaded.....

all i want is to save the original file so that i can call it and display

thanks for entertaining my question :D

Re: uploading file.... problem.... need help

Posted: Mon Feb 20, 2012 9:22 am
by Celauran
Are you saying you want to save the actual file in the database, and not just a reference to its location? 'Cause that's not a good idea....

Re: uploading file.... problem.... need help

Posted: Mon Feb 20, 2012 9:30 am
by makuletz27
if it possible.....
but you said that it's not good idea...
so what will i do...
can you guide me how to do it ?

Re: uploading file.... problem.... need help

Posted: Mon Feb 20, 2012 10:00 am
by social_experiment

Code: Select all

$location = 'upload/upload_files';
if(move_uploaded_file($tym_name, $location."/".$name)){
It looks like you are already uploading the file/s

Re: uploading file.... problem.... need help

Posted: Mon Feb 20, 2012 10:09 am
by makuletz27
i already save it in the folder....
but i want to save it to the database so i can call it when i need it...

Re: uploading file.... problem.... need help

Posted: Mon Feb 20, 2012 3:14 pm
by social_experiment
http://www.hockinson.com/programmer-web ... a.php?s=47
Have a look at this url; it explains why it is a bad idea to upload files into the database

Re: uploading file.... problem.... need help

Posted: Mon Feb 20, 2012 10:10 pm
by makuletz27
i already solve my problem ...
i create

Code: Select all

$image_path = $location."/".$name;
and save it to the database...

Code: Select all

$sql ="INSERT INTO profile_photo (id,filename,size,image_path)"."VALUES('".$_SESSION['id']."','$name','$size','$image_path')";
and call it

Code: Select all

<img name="student_photo" src="<?php
	include ("db-connect.php");

		$sql = mysql_query("SELECT * FROM profile_photo where id ='".$_SESSION['id']."'")
		or die(mysql_error);	
		
		while($info = mysql_fetch_array($sql)){
			echo $info['image_path'];
		}
		
	?>" width="85" height="85" alt="no image" />

THANKS to those who reply at my post :D :)