0

I am searching that for at least half an hour but I am not able to figure it out what the issue with my code?

Here it is:

<?php
$file = $_FILES["file"];
$filename = $_FILES["file"]["name"];
$tempdir = $_FILES["file"]["tmp_name"];
$error = $_FILES["file"]["error"];
$type = $_FILES["file"]["type"];
$size = $_FILES["file"]["size"];

$maxsize = 524288;
$allowedtypes = array("image/png", "image/jpg", "image/jpeg", "image/bmp");

$errormsg = "";
if(!empty($error))
{
    switch($error)
    {
        case '1':
            $errormsg = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
            break;
        case '2':
            $errormsg = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
            break;
        case '3':
            $errormsg = 'The uploaded file was only partially uploaded';
            break;
        case '4':
            $errormsg = 'No file was uploaded.';
            break;
        case '6':
            $errormsg = 'Missing a temporary folder';
            break;
        case '7':
            $errormsg = 'Failed to write file to disk';
            break;
        case '8':
            $errormsg = 'File upload stopped by extension';
            break;
        default:
            $errormsg = 'No error code avaiable';
    }
} elseif(empty($tempdir) || $tempdir == 'none') {
    $errormsg = 'No file was uploaded..';
} elseif(!in_array($type, $allowedtypes) || $size > $maxsize) {
    $errormsg = 'Either image type not supported or size is extending 512 KB';
} else {
    $filename = $_SESSION["username"];
    $extension = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
    $path = 'profile_pictures/' . $filename . "." . $extension;
    foreach($ext as $allowedtypes) {
        if(file_exists($filename . "." . $ext)) unlink($filename . "." . $ext);
    }
    move_uploaded_file($tempdir, $path);

    //for security reason, we force to remove all uploaded file
    @unlink($file);
}

echo $path;
?>

If I remove the foreach loop it works but it's not working with that!

Why is that happening? Please help.

Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113

1 Answers1

4

The arguments for foreach loop is incorrect. The correct syntax is:

foreach (array_expression as $value) {
    statement;
}

$ext is a string containing the extension of the uploaded file, and $allowedtypes is the array containing the valid extensions. You're trying to loop over a string.

foreach($ext as $allowedtypes) {
    if(file_exists($filename . "." . $ext)) unlink($filename . "." . $ext);
}

should be:

foreach($allowedtypes as $ext) {
    if(file_exists($filename . "." . $ext)) unlink($filename . "." . $ext);
}

Note: I suggest you enable error reporting to discover errors like this. It'd help during the development process. Also, don't use @ -- it hides useful error messages. If you don't want to display error messages to the user, turn off error reporting and log them instead.

Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • @MohammadAreebSiddiqui: `E_NOT_WORKING` isn't a valid PHP error message. **Turn on error reporting first** (see the link in my answer) and see if you get any errors. – Amal Murali Dec 19 '13 at 11:11