1

所以我正在为自己制作一个小脚本,在那里我可以将文件上传到我的服务器。它现在工作得很好,但是每当我尝试上传更大的文件时,它就会崩溃。所以我想如果我添加 ajax 支持来使它: 1. 工作得更好;2. 看起来更好。

所以,这就是我到目前为止所拥有的:

<html>
<head>
<title>File Uploader v1.0</title>
<link rel='stylesheet' href='style.css'>
<a href="index.php"><img src="img/logo.png"></a>
</head>
<body>
  <center>
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
      <tr>
         <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
         <td>
           <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
             <tr>
                <b>Please choose a file:</b><br/>
                <input type="file" name="fileup"/><br/>
                <br/>
                <input type="submit" name='submit' value="Upload"/>
             </tr>
        </form>
     </table>
</body>
</html>

<?php
    $uploadpath = 'upload/';        // directory to store the uploaded files
    $max_size = 103000000;          // maximum file size, in KiloBytes
    $allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png', 'rar', 'zip', 'exe', 'psd');        // allowed extensions

    if ( isset( $_FILES['fileup'] ) && strlen( $_FILES['fileup']['name'] ) > 1 ) {
       $uploadpath = $uploadpath . basename( $_FILES['fileup']['name'] );               // gets the file name
       $sepext = explode('.', strtolower( $_FILES['fileup']['name'] ) );
       $type = end($sepext);       // gets extension
       list ( $width, $height ) = getimagesize( $_FILES['fileup']['tmp_name'] );       // gets image width and height
       $err = '';         // to store the errors

       // Checks if the file has allowed type, size, width and height (for images)

       if ( !in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';
       if ( $_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB.';

       // If no errors, upload the image, else, output the errors

       if ( $err == '' ) {
          $i = 1;
          while ( file_exists( $uploadpath ) ) {
              //get filename without suffix
              $rootname = basename( $_FILES['fileup']['name'], $type );
              $uploadpath = "upload/" . $rootname . "-$i." . $type;
              $i++;
          }
          if ( move_uploaded_file( $_FILES['fileup']['tmp_name'], $uploadpath ) ) {
              echo '<font color="green"><b>Success!</b></font>';    
              echo '<br/>File: <b>'. basename( $_FILES['fileup']['name']). '</b>';
              echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';
              echo '<br />Size: <b>'. number_format($_FILES['fileup']['size']/1024, 3, '.', '') .'</b> KB';
              echo '<br/><br/>File path: <input type="text" value="http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath.'" readonly>';
          } else echo '<b>Unable to upload the file.</b>';
       } else echo $err;
    }
?>
</center>

因此,如果你们中的任何人能帮助我,那就太好了。谢谢!:)

4

1 回答 1

3

如果您认为在使用 ajax/javascript 脚本时不会崩溃,那么您就错了。问题不在于代码的工作。问题是 PHP 处理的最大上传大小。

您需要在 php.ini 中设置 upload_max_filesize 和 post_max_size 的值:

; Maximum allowed size for uploaded files.
upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize
post_max_size = 40M

如果您没有访问 php.ini 的权限,请要求服务提供商增加上传的大小。您不能在运行时更改这些值;当执行到达您对 ini_set 的调用时,上传大于 php.ini 中指定值的文件将失败。

请参阅核心 php.ini 指令的说明

于 2013-10-15T05:13:05.633 回答