Michael
Michael

Reputation: 13

Prevent empty data in $_FILES upload

So I've been working on this thing for a few hours now and I'm worn out. When I try and update my contact information, and I choose a picture file it uploads the number 1, to my database, when I don't choose a picture I've succeeded in making the database keep the original picture file.

Can anybody help me figure this one out?

if (!empty($_FILES["kon_up_path"]["name"]))
{
    $kon_up_path = move_uploaded_file($_FILES["kon_up_path"]["tmp_name"],"C:/xampp/htdocs/hansenservice/img/img/site/profil_pics/" . $_FILES["kon_up_path"]["name"]);
}
else
{
    $kon_up_path = $kontakt_row['cont_img'];
}

Upvotes: 1

Views: 125

Answers (5)

Ateoa
Ateoa

Reputation: 116

$kon_up_path = move_uploaded_file($_FILES["kon_up_path"]["tmp_name"],"C:/xampp/htdocs/hansenservice/img/img/site/profil_pics/" . $_FILES["kon_up_path"]["name"]);

move_uploaded_file() this function return false / ture;

You can do like this:

if(move_uploaded_file($_FILES["kon_up_path"]["tmp_name"],"C:/xampp/htdocs/hansenservice/img/img/site/profil_pics/" . $_FILES["kon_up_path"]["name"])){
    $kon_up_path = C:/xampp/htdocs/hansenservice/img/img/site/profil_pics/" . $_FILES["kon_up_path"]["name"];
}

Upvotes: 0

user149341
user149341

Reputation:

Here's your problem:

$kon_up_path = move_uploaded_file(...);

move_uploaded_file() returns TRUE on success, and FALSE on failure. It does not return a path. If you want the path of the file, use the value you passed as the second argument of move_uploaded_file().

Bonus gotcha: you need to verify that the provided filename is acceptable. The code you've provided doesn't show you checking that it has an appropriate extension, doesn't contain any invalid characters, and doesn't already exist. If you're already doing this outside this snippet, great; if not, you need to, lest a user upload a file named ../../../../index.php (for instance) and blow away your site. Consider generating the filename automatically rather than letting the user specify it at all.

Upvotes: 2

Hector Saenz
Hector Saenz

Reputation: 1

try

if (count($_FILES) >= 1)

{
    $kon_up_path = move_uploaded_file($_FILES["kon_up_path"]["tmp_name"],"C:/xampp/htdocs/hansenservice/img/img/site/profil_pics/" . $_FILES["kon_up_path"]["name"]);
}
else
{
    $kon_up_path = $kontakt_row['cont_img'];
}

regards

Upvotes: -1

Affan
Affan

Reputation: 1140

i think first print_r($_FILES) the error part contain non zero value so each time it will execute if this will happen then u have to

if($_FILES["kon_up_path"]["name"]['error'] == 0 ){
   your code 
 }

Upvotes: 0

Rafael
Rafael

Reputation: 7746

if(is_uploaded_file($_FILES['kon_up_path']['tmp_name'])) {
  //code here
}

Upvotes: 0

Related Questions