2014-07-25 3 views
1

내 PDF 업로드 스크립트에 2 가지 문제가 있습니다.PHP로 PHP 업로드 관련 문제

  1. 특정 PDF 파일 만 업로드되며 일부 PDF 파일은 업로드되지 않습니다. 나는 일하는 사람이 몇 백 킬로바이트 미만이라는 것을 알아 챘다. MB에없는 것들. 이것을 바꿀 수있는 방법이 있습니까?

  2. PDF 파일이 업로드되고 이름에 공백이 있으면 링크가 끊어집니다. html로 올바르게 지시 할 수 있도록 인코딩하는 방법이 있습니까? 구글을 통해 검색을 시도하고 아무것도 찾을 수 없습니다.

    <?php 
    include_once 'includes/db_connect.php'; 
    include_once 'includes/psl-config.php'; 
    
    $title = $_POST['title']; 
    $id = $_POST['id']; 
    
    if ($_FILES["file"]["size"] > 0) 
    { 
        $targetfolder = "uploads/"; 
    
        $targetfolder = $targetfolder . basename($_FILES['file']['name']) ; 
    
        $fileName = basename($_FILES['file']['name']) ; 
    
        $url = $_POST['url']; 
    
        $file_type=$_FILES['file']['type']; 
    
        echo "hiii"; 
    
         if ($file_type=="application/pdf" || $file_type=="application/x-pdf") 
         { 
          unlink($url); 
    
          if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder)) 
          { 
    
           if ($insert_stmt = $mysqli->prepare("UPDATE content SET title = ?, fileName = ?, url = ? WHERE id = ?;")) 
           { 
            $insert_stmt->bind_param('sssd', $title, $fileName, $targetfolder, $id); 
             if (! $insert_stmt->execute()) 
             { 
              echo "Error processing your request"; 
             } 
            echo "<p>The file is updated</p>"; 
            echo"<p>Return to the <a href='protected_page.php'>content page</a></p>"; 
           } 
           else 
           { 
            echo "<p> Failed to upload to the server</p> 
            <p> Return back to <a href='edit_content.php'>edit page</a></p>"; 
           } 
    
          } 
    
          else 
          { 
           echo "<p>Problem uploading file</p>"; 
          } 
    
         } 
    
         else 
         { 
          echo "<p>You may only upload PDFs</p> 
           <p> Return back to <a href='edit_content.php'>edit page</a></p> 
           "; 
         } 
    } 
    else 
    { 
        if ($insert_stmt = $mysqli->prepare("UPDATE content SET title = ? WHERE id = ?;")) 
        { 
         $insert_stmt->bind_param('sd', $title, $id); 
         if (! $insert_stmt->execute()) 
         { 
          header('Location: ../error.php?err=Registration failure: update'); 
         } 
          echo "<p>The file was updated</p>"; 
          echo"<p>Return to the <a href='protected_page.php'>content page</a></p>"; 
        } 
        else 
        { 
         echo "<p> Failed to upload to the server</p> 
          <p> Return back to <a href='add_content.php'>edit page</a></p>"; 
        } 
    
    } 
    ?> 
    

    는 PHP 코드의 echo "hiii"; 있습니다 : 희망 너희들은 여기

내 PHP는 ...이 되거 수 있습니다. 허용되지 않는 PDF 파일이있는 경우에는 반향하지 않으므로 파일 크기와 관련 있다고 가정합니다. 여기 는 HTML입니다 : 문제 # 2의

<?php 
include_once 'includes/db_connect.php'; 
include_once 'includes/psl-config.php'; 
include_once 'includes/functions.php'; 

sec_session_start(); 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Secure Login: Edit</title> 
     <script type="text/JavaScript" src="js/sha512.js"></script> 
     <script type="text/JavaScript" src="js/forms.js"></script> 
     <script type="text/Javascript" src="js/jQuery-1.7.js"></script> 
     <link rel="stylesheet" href="styles/main.css" /> 
    </head> 
    <body> 
     <?php if (login_check($mysqli) == true) : ?> 
      <?php 
       $username = htmlentities($_SESSION['username']); 
       $privileges = mysqli_query($mysqli, "Select privileges From members Where privileges = 'Admin' AND username = '$username' LIMIT 1"); 
       $rowPriv =mysqli_fetch_array($privileges); 

       if ($rowPriv[0] == "Admin") 
       { 
        $id = $_GET['id']; 
        $editRow = mysqli_query($mysqli, "Select ID, title, url, fileName From content Where ID = $id;"); 
        echo "<h1>Edit Content</h2>"; 

         while ($rowEdit = mysqli_fetch_array($editRow)) 
         { 
         echo "<form action='edit_file.php' method='post' id='editForm' enctype='multipart/form-data'> 

          <table cellspacing='10'> 
           <tr> 
            <td> 
             Title: 
            </td> 
            <td> 
             <input name='title' id='title' type='text' value='".$rowEdit['title']."' size='40' > 
            </td> 
           </tr> 
           <tr> 
            <td> 
             Upload Different File 
            </td> 
            <td> 
             <input type='checkbox' id='checkbox' checked> 
            </td> 
           </tr> 
           <tr id='file'> 
            <td> 
             File: 
            </td> 
            <td> 
             <input type='file' name='file' size='50'/> 
            </td> 
           </tr> 
           <tr> 
            <td> 
             <input name='id' type='hidden' value='".$rowEdit['ID']."'> 
            </td> 
           </tr> 
           <tr> 
            <td> 
             <input name='url' type='hidden' value='".$rowEdit['url']."'> 
            </td> 
           </tr> 
          </table>"; 
          } 
          echo "<br />       
          <input type='button' id='edit' value='Submit' /> 
         </form> 
         <script> 

          $('#edit').click(function(){ 
           var title = $('#title').val(); 
           if (title != '') 
           { 
            $('#editForm').submit(); 
            return true; 
           } 
           else{ 
            alert('Must enter a Title'); 
            return false; 
           } 
          }); 
          $('#checkbox').live('change', function(){ 
            if ($(this).is(':checked')) { 
             $('#file').show(); 
            } else { 
             $('#file').hide(); 
            } 
          }); 

         </script>"; 



       } 
       else 
       { 
        echo "<p><span>Sorry, you do not have privileges</span></p>"; 
       } 


      echo"<p>Return to the <a href='protected_page.php'>content page</a>.</p> 
      <p>Return to the <a href='login.php'>login page</a>.</p>"; 
     ?> 
     <?php else : ?> 
      <p> 
       <span class="error">You are not authorized to access this page.</span> Please <a href="login.php">login</a> with a privileged user. 
      </p> 
     <?php endif; ?> 
    </body> 
</html> 
+1

업로드 크기 문제 ... http://stackoverflow.com/questions/727736/php-maximum-total-upload-size/727759#727759 – DragonYen

+0

문제 # 1이 수정되었습니다. 사이드 노트를 위해서 php.ini 파일에 변경 사항을 적용하기 위해 아파치를 다시 시작해야했습니다. –

답변

1

당신은 그래서 당신은 깨진 링크를받지 않습니다 밑줄 공간을 대체 업로드 된 파일의 이름을 바꿀 수 있습니다

$targetfolder = $targetfolder . basename($_FILES['file']['name']); 

될 것

$targetfolder = $targetfolder . basename(str_replace(' ', '_', $_FILES['file']['name'])); 
+0

나는 다른 방법을 찾아 냈습니다. 난 그냥 'rawurlencode' –