上传使用PHP脚本文件,而无需离开该网页脚本、上传、网页、文件

2023-09-10 14:58:21 作者:往事長出胡子

我想使用PHP脚本上传HTML文件。

I am trying to upload a file in html using php script.

<html>
<body>
  <form id='importPfForm' enctype="multipart/form-data" action="http://localhost/js/upload.php" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
    Choose a file to upload: <input name="uploaded_file" type="file" />
    <input type="submit" value="Upload" />
  </form>
</body>
</html>

和upload.php的包含以下code:

and the "upload.php" contains the following code:

<?php
$upload_key = 'uploaded_file';
if (isset($_FILES[$upload_key])) {
    try {
        $error = $_FILES[$upload_key]['error'];
        switch ($error) {
            case UPLOAD_ERR_INI_SIZE:
                throw new Exception('Exceeded upload_max_filesize');
            case UPLOAD_ERR_FORM_SIZE:
                throw new Exception('Exceeded MAX_FILE_SIZE');
            case UPLOAD_ERR_PARTIAL:
                throw new Exception('Incomplete file uploaded');
            case UPLOAD_ERR_NO_FILE:
                throw new Exception('No file uploaded');
            case UPLOAD_ERR_NO_TMP_DIR:
                throw new Exception('No tmp directory');
            case UPLOAD_ERR_CANT_WRITE:
                throw new Exception('Can\'t write data');
            case UPLOAD_ERR_EXTENSION:
                throw new Exception('Extension error');
        }
        $finfo    = new finfo(FILEINFO_MIME);
        $name     = $_FILES[$upload_key]['name'];
        $tmp_name = $_FILES[$upload_key]['tmp_name'];
        $size     = $_FILES[$upload_key]['size'];
        if ($size > 350000)
            throw new Exception('Exceeded 350KB limit');
        if (!is_uploaded_file($tmp_name))
            throw new Exception('Not an uploaded file');
        $type = $finfo->file($tmp_name);
        if ($type === false)
            throw new Exception('Failed to get MimeType');
        if ($type !== 'text/plain; charset=us-ascii')
            throw new Exception('Only csv available');
        $new_name = dirname(__FILE__).'/upload/'.$name;
    echo  $new_name;
        if (is_file($new_name))
            throw new Exception("The file {$new_name} already exists");
        if (!move_uploaded_file($tmp_name,$new_name))
            throw new Exception('Failed to move uploaded file');
        echo "File successfully uploaded as {$new_name}";
    } catch (Exception $e) {
        echo 'Error: '.$e->getMessage();
    }
}
?>

但这种方法会打开一个新的网页。我想不留网页执行的功能,我需要使用HTML页面中的变量 $ NEW_NAME 。什么是我需要在我的html页面进行修改?我是pretty的肯定通过某种形式的Ajax请求的事情这工作。但我不知道我在说什么。我不是大了Ajax或JavaScript,但是这是一个功能,我使用非常频繁,我想了解它是如何工作的,所以我可以实现它,当我需要现在和未来。

But this method opens a new web page. I want to perform the function without leaving the web page and i need to use the variable $new_name in html page. What is the modification i need to perform in my html page? I'm pretty sure this works via some kind of ajax request thing. but I have no idea what I'm talking about. I'm not big on ajax or javascript, but this is a function i use very frequently and I'd like to learn how it works so I can implement it when i need to now and in the future.

推荐答案

有两种选择真的在这里。您可以使用下面的code作为形式的操作,然后在同一个文件中的PHP。

There are two choices really here. You could use the following code as the action for the form and then have the php in the same file.

<?php echo $_SERVER['PHP_SELF']; ?>

header("Location: upload.html?new_name=value_of_new_name");

在upload.php的,其中其成功,那么在upload.html你可以使用 $ _ GET ['NEW_NAME'] 来检索的值 $ NEW_NAME

In the upload.php where its successful, then in upload.html you can use $_GET['new_name'] to retrieve the value of $new_name