PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
makuletz27
Forum Newbie
Posts: 11 Joined: Fri Feb 17, 2012 1:53 am
Post
by makuletz27 » Mon Feb 20, 2012 2:00 am
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>
Last edited by
makuletz27 on Mon Feb 20, 2012 8:03 am, edited 1 time in total.
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Mon Feb 20, 2012 3:04 am
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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
makuletz27
Forum Newbie
Posts: 11 Joined: Fri Feb 17, 2012 1:53 am
Post
by makuletz27 » Mon Feb 20, 2012 7:43 am
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....
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Mon Feb 20, 2012 9:08 am
makuletz27 wrote: it aggravated the code....
how
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
makuletz27
Forum Newbie
Posts: 11 Joined: Fri Feb 17, 2012 1:53 am
Post
by makuletz27 » Mon Feb 20, 2012 9:18 am
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
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Mon Feb 20, 2012 9:22 am
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....
makuletz27
Forum Newbie
Posts: 11 Joined: Fri Feb 17, 2012 1:53 am
Post
by makuletz27 » Mon Feb 20, 2012 9:30 am
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 ?
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Mon Feb 20, 2012 10:00 am
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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
makuletz27
Forum Newbie
Posts: 11 Joined: Fri Feb 17, 2012 1:53 am
Post
by makuletz27 » Mon Feb 20, 2012 10:09 am
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...
makuletz27
Forum Newbie
Posts: 11 Joined: Fri Feb 17, 2012 1:53 am
Post
by makuletz27 » Mon Feb 20, 2012 10:10 pm
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