Hi All!
I got a problem with one of my files where I can't upload a file to my database. I want to save the path in the db but it gives me an error and says the "Filename cannot be empty".
In my .html
Upload a profile picture(50px X 50px): <td><input type="file" name="file" value="Choose a file" id="file"/></td>
And then I send it to my .php file via <form method="post" action="index.php" enctype="multipart/form-data">
In my php I test if it has an jpeg,png etc. extention
$image = $_FILES['file']['name'];
$error = false;
if($image){
$filename = stripslashes($_FILES['file']['name']);
$lastpart = getLastExtention($filename);
$lastpart = strtolower($lastpart);
if (($lastpart != "jpg") && ($lastpart != "jpeg") && ($lastpart != "png") && ($lastpart != "gif"))
{
echo "<script type='text/javascript'> alert('Please upload a valid profile picture!'); </script>";
$error = true;
}
else
{
$image_name= $userName.'.'.$lastpart;
$newname="Images/".$image_name;
}
}
if(!$error){
$data = copy($_FILES['file']['tmp_name'],$newname);
$result = "UPDATE mytable SET Profile_Picture = '$newname' WHERE Name = '$theName'";
if(!$result){
echo "<script type='text/javascript'> alert('Please upload a valid profile picture!'); </script>";
}
}
function getLastExtention($string){
$dot = strpos($string,".");
if(!$dot){
return "";
}
$len = strlen($string) - $dot;
$extent_part = substr($string,$dot+1,$len);
return $extent_part;
}
if($image){
$query = sprintf("INSERT INTO mytable(Profile Picture) VALUES ('$image')");
}
else{
echo "No Profile picture was uploaded!";
}
I think I'm just missing something small! Or is there another way to move the uploaded file to a specific directory?
Any help welcome :D - still learning!
P