1

I want to upload an file from form and when I click submit it uploads the file to the server and insert filename, filesize and filetype from that file. but I always get errors like:
Warning: "filesize(): "stat failed for "uploadedfile" in C:\wamp\www\Project2\members\uploaded.php on line 14"
Warning: filetype(): "Lstat failed for "uploadedfile" in C:\wamp\www\Project2\members\uploaded.php on line 15"
Here is my uploadfile.php:

<html>
<body>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<title>Uploading Page</title>
<b>Hello again,<?php echo $username ?>. From here, you can select your image or file and upload our database.</b>
<form action="uploaded.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form></tr>
</table>
</form>
</body>
</html>

and here is my uploaded.php:

<html>
<body>
<title>Uploading Page</title>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $filename = basename($_FILES["fileToUpload"]["name"]);
  $filesize = filesize($_FILES["fileToUpload"]["name"]);
  $filetype = filetype($_FILES["fileToUpload"]["name"]);

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "<br>File is an image - " . $check["mime"] . ".</br>";
        $uploadOk = 1;
    } else {
        echo "<br>File is not an image.</br>";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "<br>Sorry, file already exists.</br>";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000000000000) {
    echo "<br>Sorry, your file is too large.</br>";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "<br>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</br>";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "<br>Sorry, your file was not uploaded.</br>";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
       $sql=mysqli_query($con,"insert into datas(username,imagesnotes,size,type) values('$username','$filename','$filesize')");
    if(!$sql)
    {
    echo "<br>there is a problem in MySQL please check again.</br>";
    }
       echo "<br>The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.</br>";
    } else {
        echo "<br>Sorry, there was an error uploading your file.</br>";
    }
}
mysqli_close($con);
?>

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.