Php 将单张照片上传转换为多张照片上传(支持iPhone)

Php 将单张照片上传转换为多张照片上传(支持iPhone),php,file-upload,Php,File Upload,我学习了一个教程,帮助我制作了一个非常基本的表单,允许用户将图像上传到我的服务器(以及他们的手机) 我想允许多个文件上传,但我不知道如何转换我的PHP脚本 如何将下面的工作代码转换为单张照片上传到多张照片上传 以下是我的代码的相关部分: index.php: <form action="upload.processor.php" enctype="multipart/form-data" id="upload" method="post" name="Upload"> <

我学习了一个教程,帮助我制作了一个非常基本的表单,允许用户将图像上传到我的服务器(以及他们的手机)

我想允许多个文件上传,但我不知道如何转换我的PHP脚本

如何将下面的工作代码转换为单张照片上传到多张照片上传

以下是我的代码的相关部分:

index.php:

<form action="upload.processor.php" enctype="multipart/form-data" id="upload" method="post" name="Upload">
   <input id="file" name="file" type="file" accept="image/*" capture="camera" />
   <input id="submit" name="submit" type="submit" value="Upload!">
</form>
<?php 

    // make a note of the current working directory, relative to root. 
    $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']); 

    // make a note of the directory that will receive the uploaded file 
    $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'img/'; 

    // make a note of the location of the upload form in case we need it 
    $uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'index.php'; 

    // make a note of the location of the success page 
    $uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.success.php'; 

    // fieldname used within the file <input> of the HTML form 
    $fieldname = 'file'; 

    // possible PHP upload errors 
    $errors = array(1 => 'php.ini: Max file size exceeded.', 
                    2 => 'HTML form: Max file size exceeded.', 
                    3 => 'File upload was only partial.', 
                    4 => 'No file was attached.'); 

    // check the upload form was actually submitted else print the form 
    isset($_POST['submit']) 
        or error('The upload form is required.', $uploadForm); 

    // check for PHP's built-in uploading errors 
    ($_FILES[$fieldname]['error'] == 0) 
        or error($errors[$_FILES[$fieldname]['error']], $uploadForm); 

    // check that the file we are working on really was the subject of an HTTP upload 
    @is_uploaded_file($_FILES[$fieldname]['tmp_name']) 
        or error('Not an HTTP upload.', $uploadForm); 

    // validation to make sure the uploaded file is in fact an image. 
    @getimagesize($_FILES[$fieldname]['tmp_name']) 
        or error('Only image uploads are allowed!', $uploadForm); 

    // make a unique filename for the uploaded file and check it is not already 
    // taken... if it is already taken keep trying until we find a vacant one 
    // sample filename: 1140732936-filename.jpg 
    $now = time(); 
    while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name'])) { 
        $now++; 
    } 

    // move the file to its final location and allocate the new filename to it 
    @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename) 
        or error('Receiving directory has incorrect permissions.', $uploadForm); 

?> 

upload.processor.php:

<form action="upload.processor.php" enctype="multipart/form-data" id="upload" method="post" name="Upload">
   <input id="file" name="file" type="file" accept="image/*" capture="camera" />
   <input id="submit" name="submit" type="submit" value="Upload!">
</form>
<?php 

    // make a note of the current working directory, relative to root. 
    $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']); 

    // make a note of the directory that will receive the uploaded file 
    $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'img/'; 

    // make a note of the location of the upload form in case we need it 
    $uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'index.php'; 

    // make a note of the location of the success page 
    $uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.success.php'; 

    // fieldname used within the file <input> of the HTML form 
    $fieldname = 'file'; 

    // possible PHP upload errors 
    $errors = array(1 => 'php.ini: Max file size exceeded.', 
                    2 => 'HTML form: Max file size exceeded.', 
                    3 => 'File upload was only partial.', 
                    4 => 'No file was attached.'); 

    // check the upload form was actually submitted else print the form 
    isset($_POST['submit']) 
        or error('The upload form is required.', $uploadForm); 

    // check for PHP's built-in uploading errors 
    ($_FILES[$fieldname]['error'] == 0) 
        or error($errors[$_FILES[$fieldname]['error']], $uploadForm); 

    // check that the file we are working on really was the subject of an HTTP upload 
    @is_uploaded_file($_FILES[$fieldname]['tmp_name']) 
        or error('Not an HTTP upload.', $uploadForm); 

    // validation to make sure the uploaded file is in fact an image. 
    @getimagesize($_FILES[$fieldname]['tmp_name']) 
        or error('Only image uploads are allowed!', $uploadForm); 

    // make a unique filename for the uploaded file and check it is not already 
    // taken... if it is already taken keep trying until we find a vacant one 
    // sample filename: 1140732936-filename.jpg 
    $now = time(); 
    while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name'])) { 
        $now++; 
    } 

    // move the file to its final location and allocate the new filename to it 
    @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename) 
        or error('Receiving directory has incorrect permissions.', $uploadForm); 

?> 

您将迭代$\u文件[$fieldname],以获得基于表单的文件数组。因此,迭代数组,然后处理文件

因此:


这是按原样工作,还是需要进行调整?我把它放进去了,它似乎不起作用。我已经更新了代码。它意味着替换以下所有内容:isset($_POST['submit'])或error('需要上传表单',$uploadForm);