duanche4578 2018-07-05 08:55
浏览 88
已采纳

php上传不起作用后删除文件名中的空格

I have a working php upload script but the line to rename the file after the upload is ignored/not working.

This is the relevant part:

        if (!file_exists($uploaddir . $_FILES["file"]["name"]))
        {
            // Proceed with file upload
            if (is_uploaded_file($_FILES['file']['tmp_name']))
            {
                //File was uploaded to the temp dir, continue upload process
                if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $_FILES['file']['name']))
                {
                    //chown ($uploaddir . $_FILES['file']['name'], 'www-data');
                    //chmod ($uploaddir . $_FILES['file']['name'], 0755);
                    $name = str_replace(" ", "_", $uploaddir . $_FILES['file']['name']);
                    // uploaded file was moved and renamed succesfuly. Display a message.
                    // Now log the uploaders IP adress date and time
                    $date = date("m/d/Y"); 
                    $time = date("h:i:s A");                
                    $fp = fopen($log,"ab"); 
                    fwrite($fp,"$ip | ".$_FILES['file']['name']." | $date | $time | OK"."
"); 
                    fclose($fp); 

                    $to = 'mail@sjn.net';
                    $subject = 'Upload Completed'; 
                    $headers = "From: mail@sjn.net
Content-Type: text/html; charset=iso-8859-1";

                    ob_start();
                ?>

                    <p>A file has been succesfully uploaded! - https://www.sjn.net/upload/<?php echo $_FILES['file']['name']; ?></p>

                <?php
                    $message = ob_get_clean();
                    $mail_sent = @mail( $to, $subject, $message, $headers );
                    echo "Upload erfolgreich! Link zur Datei:<br><input size='50' type='text' value='https://www.sjn.net/upload/".$_FILES['file']['name']."' id='myInput'><button onclick='myFunction()'>in Zwischenablage</button><script>function myFunction() {var copyText = document.getElementById('myInput');copyText.select();document.execCommand('copy');}</script>";

                }

As you can see i already tried to work on the permissions but did not change. What could it be that the file is not renamed replacing spaces?

  • 写回答

1条回答 默认 最新

  • duanbai1027 2018-07-05 09:15
    关注

    Try with below code, I have just changed and replaced the space with underscore before move_uploaded_file() function and used that variable. Try with this if its work for you.

       if (!file_exists($uploaddir . $_FILES["file"]["name"]))
        {
            if (is_uploaded_file($_FILES['file']['tmp_name']))
            {
                $filename = str_replace(' ', '_', $_FILES['file']['name']);
    
                if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $filename))
                {
                    $name = str_replace(" ", "_", $uploaddir . $filename);
    
                    $date = date("m/d/Y"); 
                    $time = date("h:i:s A");                
                    $fp = fopen($log,"ab"); 
                    fwrite($fp,"$ip | ".$filename." | $date | $time | OK"."
    "); 
                    fclose($fp); 
    
                    $to = 'mail@sjn.net';
                    $subject = 'Upload Completed'; 
                    $headers = "From: mail@sjn.net
    Content-Type: text/html; charset=iso-8859-1";
    
                    ob_start();
                ?>
    
                    <p>A file has been succesfully uploaded! - https://www.sjn.net/upload/<?php echo $filename; ?></p>
    
                <?php
                    $message = ob_get_clean();
                    $mail_sent = @mail( $to, $subject, $message, $headers );
                    echo "Upload erfolgreich! Link zur Datei:<br><input size='50' type='text' value='https://www.sjn.net/upload/".$filename."' id='myInput'><button onclick='myFunction()'>in Zwischenablage</button><script>function myFunction() {var copyText = document.getElementById('myInput');copyText.select();document.execCommand('copy');}</script>";
    
                }
            }
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?