0

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

3
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by diafol
0

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

0

"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!

1

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
}

Edited by diafol: n/a

0

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

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

Will post when I succeed!

Edited by Phillamon: n/a

0

,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";

}

Edited by veedeoo: n/a

0

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
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.