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.

Apoorv Pal
  • 197
  • 3
  • 14
  • You need to call your page php in your form tag `
    `
    –  Oct 25 '17 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. – José A. Zapata Oct 25 '17 at 16:42

1 Answers1

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);
Ori a
  • 314
  • 1
  • 8
  • 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. – Apoorv Pal Oct 25 '17 at 16:49
  • you can find a full example about how to upload file using php here: https://www.w3schools.com/php/php_file_upload.asp – Ori a Oct 25 '17 at 16:59
  • if your asking about how to remove the file limit - check the link in my answer. – Ori a Oct 25 '17 at 17:00
  • 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 – Apoorv Pal Oct 25 '17 at 17:02
  • 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); – Ori a Oct 25 '17 at 17:09
  • I just asked the server admin of my shared hosting plan. The host has set it upload_max_filesize to 2MB causing the problem. Will sort it out. Anyways, thanks for all the help guys! – Apoorv Pal Oct 25 '17 at 17:14
  • Glad to help, if this answer solved your problem please mark it as accepted by clicking the check mark next to the answer. see: How does accepting an answer work? for more information – Ori a Oct 25 '17 at 17:16