0

PHP Code:

if(isset($_POST["btn-vd-submit"]) AND $vd_perm_actual > 0) {
$filename = $_FILES['vdfile']['name'];
$target_dir = "./voice-demo-files/";
$target_file = $target_dir . basename($_FILES['vdfile']['name']);
$uploadOk = 1;
$vdFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES['vdfile']['size'] > 50000000000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($vdFileType != "mp3") {
    echo "Sorry, only mp3 files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES['vdfile']['tmp_name'], $target_file)) {
        echo "The file ". basename( $_FILES['vdfile']['name']). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

HTML Form:

<form class="custom-form" method="post" enctype="multipart/form-data">
    <div class="s-4 m-4 center">
        <center>Choose A Voice Demo File (.mp3) to upload and link with your profile:<br><br><br>
        <input type="file" name="vdfile" id="vdfile"><br><br><br>
    </div>
    <div class="s-4 m-4 center">
        <button class="submit-form center button background-primary text-white" name="btn-vd-submit" type="submit">Upload This Voice Demo!</button>
    </div>

When I select and upload a file, I get the custom error message I set, "Sorry, there was an error uploading your file."; I don't get any kind of php error displayed on the page or in the logs but when I check the directory the file is not uploaded.

2
  • You need to call your page php in your form tag <form action="upload.php" ...> Commented Oct 25, 2017 at 16:35
  • Check the permissions of the target directory. Assuming you're running Apache, the directory should be writable by the the user running the Apache process. If all else fails, set the target directory to be universally writable. Commented Oct 25, 2017 at 16:42

1 Answer 1

0

Try the following tests:

  1. Upload a 1kb file. if thats work, it means that you got max file limit error. (read here how to fix it)
  2. If you got during the 1kb uploading attempt try to upload file into other directory (maybe current directory) - success means you probebly can write files into the directory due permissions. read here about fixing persmissions issues

beside that , try to add the following line to your code , it will help us in the debugging process:

error_reporting(E_ALL);
Sign up to request clarification or add additional context in comments.

7 Comments

That was great, thanks! It is indeeed the file size limit. But anything wrong in the syntax? But if I comment out the check file size part, it still gives an error if I upload any other mp3 file which is not the test 1kb file.
you can find a full example about how to upload file using php here: w3schools.com/php/php_file_upload.asp
if your asking about how to remove the file limit - check the link in my answer.
Even if I remove the file limit part of the php code, the file doesn't upload and this time I didn't even get an error message. And I am using the code similar to w3schools one. Just without any size limit now
try to add this line to the top of your php code and then try to reupload , msg what msgs accure:: error_reporting(E_ALL);
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.