【问题标题】:MP3 files reduce in size and get corrupted after uploading from a formMP3 文件在从表单上传后变小并损坏
【发布时间】:2013-04-24 17:53:24
【问题描述】:

这是原始 mp3 文件:Pon-3jay LongR.mp3
这是损坏的版本:pon-3jay longr.mp3

这是移动文件的脚本:

<?php

require_once "File.php";
$path = "uploads/songs/";
$valid_formats = array("mp3");
$File = new File();

$path = $path."djdavid98/";

    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
        $name = $_FILES['song']['name'];
        $size = $_FILES['song']['size'];
        $tmp  = $_FILES['song']['tmp_name'];

        if (!is_dir($path)) mkdir($path, 0777, true);

        if($File->upload($tmp, $actual_song_name,$path)){
            echo response($path.$actual_song_name,$actual_song_name,$txt.".".$ext);
        }
        else echo errText("Upload failed!", $path );
    }

function errText($str,$fileName){
    return '{"song":{"sucess":"0","message":"' .$str. '","fileName":"' . $fileName . '"}}';
}
function response($filePath,$actualFileName,$fileName){
    return '{"song":{"sucess":"1","path":"' . $filePath . '","fileName":"' . $fileName . '"}}';
}

?>

文件.php:

<?php
    class File {
        public function upload($file, $filename, $target_path) {
            if (empty($filename)) { break; }
            try {
                $target_path = $target_path . $filename;

                if(move_uploaded_file($file, $target_path)) {
                    return true;
                } else{
                    return false;
                }
            } catch (Exception $ex) {
                echo "Error on <strong>Line " . $ex->getLine() . " </strong>: " . $ex->getMessage();
            }
        }
    }
?>

表格:

<form class="upload_form" enctype="multipart/form-data" style="height:0;opacity:0;">
    <div class="grid-100 grid-parent">
        <div class="grid-20">
            ...
            <div data-input>
                <span>Song</span>
                <div class="grid-100 grid-parent fileSelect">
                    <div class="grid-60">
                        <input type="text" disabled class="file-holder">
                    </div>
                    <div class="file-wrapper grid-40">
                        <input type="file" name="song" required>
                        <div class="button styleWarning">Browse</div>
                    </div>
                </div>
            </div>
        </div>
        <div class="grid-80 grid-parent">
            ...
        </div>
    </div>
</form>

此脚本在 CloudFlare 主机上运行,​​以防万一发生任何变化。据我所知,所有上传的 mp3 文件都作为相同的文件上传。老实说,我不知道为什么会发生这种情况以及如何解决这个问题。任何帮助表示赞赏。

【问题讨论】:

  • 非mp3文件也会发生同样的事情吗?
  • @Gavin 脚本还会上传图片文件,上传就好了。
  • 幕后是否发生了其他事情,例如 ftp 传输?
  • @Gavin 这是所有脚本。我使用的唯一 ftp 传输是上传脚本。
  • 您对服务器/主机有 root 访问权限吗?我想知道在调用 move_uploaded_file 之前文件是否已损坏?其他选项可能是出现故障的网络设备/代理,以测试从不同位置或服务器本地尝试(如果您还没有)。我想这也有可能是在主机上运行的服务,它可以更改 mp3 文件,以测试在您的机器本地或不同主机上尝试您的代码。顺便说一句,你的代码对我来说看起来不错。

标签: php file-upload corruption


【解决方案1】:

无意中保存了封面艺术(图像)文件而不是 mp3 文件。

在 ajaxprocessform.php 中引用以下行:

$tmp = $_FILES['cover']['tmp_name']; //this is the image

if (!is_dir($path)) mkdir($path, 0777, true); 

if($File->upload($tmp, $actual_song_name,$path)){ //image saved with mp3 path

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 2018-07-12
    • 2017-04-21
    相关资源
    最近更新 更多