简体   繁体   中英

Upload mp3 file into folder is not working

i'm trying to upload mp3 format file in to a folder using PHP. But this following coding is not working.Please can some one help my?

define("UPLOAD_DIR", "mp3_upload_folder/");
if (!empty($_FILES["myFile"])) 
        {
        $myFile = $_FILES["myFile"];

        if ($myFile["error"] !== UPLOAD_ERR_OK) 
            {
            echo "<p>An error occurred.</p>";
            exit;
            }
     // verify the file is a GIF, JPEG, or PNG
    $fileType = exif_imagetype($_FILES["myFile"]["tmp_name"]);

        // ensure a safe filename
        $name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);

        // don't overwrite an existing file
        $parts = pathinfo($name);

        $sanjiv=$_POST['user_id'];
        $name= $sanjiv.".".$parts["extension"];
        // preserve file from temporary directory
        $success = move_uploaded_file($myFile["tmp_name"],
            UPLOAD_DIR . $name);
        if (!$success) { 
            echo "<p>Unable to save file.</p>";
            exit;
        }

This works, and make sure that it has permissions to write to the mp3_upload_folder. I think it's still short of a complete solution, but I am lacking a full view of the problem.

<!DOCTYPE html>
<html>
<head>

</head>
<body>
<?php

define("UPLOAD_DIR", "./mp3_upload_folder/");
if (!empty($_FILES["myFile"])) {
    $myFile = $_FILES["myFile"];

    if ($myFile["error"] !== UPLOAD_ERR_OK) {
        echo "<p>An error occurred.</p>";
        exit;
    }

    // ensure a safe filename
    $name = $myFile["name"]; //preg_replace("/[^A-Z0-9\.\-]/i", "_", $myFile["name"]); //this is pointless if you're not using it

    // don't overwrite an existing file
    $parts = pathinfo($name);

    $name= $_POST['user_id'].".".$parts["extension"];
    // preserve file from temporary directory
    $success = move_uploaded_file($myFile["tmp_name"], UPLOAD_DIR .$name);
    if (!$success) { 
        echo "<p>Unable to save file.</p>";
        exit;
    }
}

?>
<form method="post" enctype="multipart/form-data">
    <input type="text" value="testUserId"  name ="user_id"/>
    <input type="file" name="myFile"/>
    <input type="submit"/>
</form>     

</body>
</html>

Also make sure that you edit your server settings as well as your php settings for uploading file sizes(in the php.ini):

Post_max_size

Upload_max_size (probably your offender default is 2M)

I'm not sure what server you are using, but information can be found easily enough by googling the server type and "max upload size"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM