如何使用php脚本将图像文件上传到远程服务器文件夹

时间:2017-06-29 08:29:44

标签: php

先生..

请帮我找到使用php脚本将图像文件从一台计算机上传到远程服务器文件夹的代码。

2 个答案:

答案 0 :(得分:1)

首先,确保将PHP配置为允许文件上传。

在你的" php.ini" file,搜索file_uploads指令,并将其设置为On:

MyDownloader

接下来,创建一个允许用户选择要上传的图像文件的HTML表单:

NSObject

并创建" upload.php"文件看起来像这样:

selector

https://www.w3schools.com/php/php_file_upload.asp

答案 1 :(得分:0)

这里的例子:你需要用文件创建输入表单。然后chek if isset,一些检查扩展和大小,然后移动它。

private function _uploadAvatar() {
        if (isset($_POST["uploadImage"])) {
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $params = explode('/', PARAMS);
            $userId = end($params);
            $temp = $_FILES["avatar"]["tmp_name"]; // Temporary name of image
            $name = $_FILES["avatar"]["name"]; // Name of image
            $size = $_FILES["avatar"]["size"]; // Size
            $ext = finfo_file($finfo, $temp);
            $uploadMaxSize = $this->convertMegabytestoBytes(ini_get('upload_max_filesize'));
            $postMaxSize = $this->convertMegabytestoBytes(ini_get('post_max_size'));
            $extensions = array("image/jpeg", "image/jpg", "image/png", "image/gif");

            if ($size > $uploadMaxSize || $size > $postMaxSize) {
                echo 'File is too big';
            }
            if (!in_array($ext, $extensions)) {
                echo 'File is not valid image';
            }
            if (empty($_SESSION['errors'])) {
                move_uploaded_file($temp, __SITE_PATH . "/pictures/" . $name);
                $path = URL . '/pictures/' . $name;
                $user = new user($this->registry);
                $user->uploadAvatar($userId, $path);
                echo 'Image was successful upload';
                header("Refresh:2");
            }
        }
    }