duanche4578
duanche4578
2018-07-05 08:55

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 duanbai1027 3年前

    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>";
    
                }
            }
    
        }
    
    点赞 评论 复制链接分享

相关推荐