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

Recommended Answers

All 7 Replies

Member Avatar for diafol

You don't show your full form - have you included enctype="multipart/form-data" attribute in the form tag?

"And then I send it to my .php file via <form method="post" action="index.php" enctype="multipart/form-data"> " is in the post... I only included the parts that is necessary though...as you can see!

Member Avatar for diafol

Ah! Sorry, I was looking at the code block, must have missed the icode. Doh! It's an age thing :)

I'd avoid using copy() until the file is located in your upload folder. You need to use move_upload_file().

Have a look at Tizag's site for a good tutorial on this:

http://www.tizag.com/phpT/fileupload.php

You can then store the filename in the DB. Should be straightforward.

$filename = $_FILES['file']['name']; //or use a custom naming system
$path = "uploads/"; //default location
$target = $path . $filename;
if(move_uploaded_file($_FILES['file']['tmp_name'],$target)){
  //success - add $filename to DB
}else{
  //upload error
}

Ahh Thank you very much! I'll check it out!

Not a problem! Just glad for some help! :)

Will post when I succeed!

,and here is how you can sort the allowed extensions. This will be based on the codes presented above.

$username= "Member Name";
$filename = strtolower($_FILES['file']['name']);
//lets get the extension of uploaded image
$ext = strtolower(substr($filename, strrpos($filename, '.') + 1));
//compare the uploaded extension to allowed extensions
$allowed_exts = array('jpg', 'gif', 'bmp', 'png');
if ((array_search($ext, $allowed_exts) !== FALSE)){
//do whatever you want here
 // can name the image file with the username of the uploader if you want
$filename = str_replace(" ", "-", $username).".".$ext;
//move the uploaded file with the username name
move_uploaded_file($_FILES['file']['tmp_name'], $path.$filename);
//insert to database

}
else{
echo "File extension NOt allowed";

}

Thanks veedeo!Going to try that aswell!

Member Avatar for diafol

For extension type, try this:

$allowed = array("gif","png","jpg","jpeg");
$filename = 'image.gif';
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if(in_array($extension,$allowed)){
   //allowed extension
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.