简体   繁体   English

将文件上传到服务器php

[英]uploading files to server php

I tested my code locally and everything works fine, I tried with 0777 and 0755 permissions on server for the folders and still the upload of a file is not working. 我在本地测试了我的代码,并且一切正常,我尝试使用服务器上的0777和0755权限访问文件夹,但文件上传仍然无法正常进行。 My code is: 我的代码是:

$ds = DIRECTORY_SEPARATOR;  

$storeFolder = '..\uploads';   

if (!empty($_FILES)) {
    foreach ($_FILES['file']['tmp_name'] as $key => $error) { 
        if ($error == UPLOAD_ERR_OK) {
            $tempFile = $_FILES['file']['tmp_name'][$key];    //temporary file                  

            $file_name = mt_rand(1000000,9999999); //generate the number for file name

            $imageName = $file_name.'.' . pathinfo($_FILES['file']['name'][$key],PATHINFO_EXTENSION); // set the new file name

            $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds; //target path where the image will be stored

            $targetFile =  $targetPath. $imageName;   // set the target path and image, url

            move_uploaded_file($tempFile,$targetFile); //move the uploaded file     

            echo 'BCK../uploads/'. $imageName;

        }
    }

}

I tried almost everything. 我几乎尝试了一切。 And uploading is still not working. 而且上传仍然无法正常进行。 This .php script returns response like this: 该.php脚本返回如下响应:

BCK../uploads/5631131.jpg 

So obviously there is a file, but it is not uploaded. 因此显然有一个文件,但是没有上传。

HTML code is: HTML代码是:

<form action="action.php" enctype="multipart/form-data" class="dropzone" style="float:left; background-image: url(design.png); ">
  <div class="fallback">
     <input name="file" type="file" multiple />
  </div>
</form>

Any help or advice would be appreciated since I now tried everything and still no result. 自从我现在尝试了一切,但仍然没有结果,任何帮助或建议都将不胜感激。

response i get local: 回复我到本地了:

tempFile C:\xampp\tmp\php89FF.tmp
file_name 4337681
imageName 4337681.jpg
targetPath  C:\xampp\htdocs\designer\pk\../uploads\
targetFile  C:\xampp\htdocs\designer\pk\../uploads\4337681.jpg
BCK../uploads/4337681.jpg 

response i get on server: 我在服务器上得到响应:

tempFile /tmp/phpjs6s9x
file_name 5394604
imageName 5394604.jpg
targetPath  /var/www/designer/pk\..\uploads\
targetFile  /var/www/designer/pk\..\uploads\5394604.jpg
BCK../uploads/5394604.jpg 

try to change this line: 尝试更改此行:

$storeFolder = '..\uploads';

to this: 对此:

$storeFolder = '..' . $ds .  'uploads';   

You need to make create your target path folders if not created and consider not creating them if already exist. 如果尚未创建,则需要创建目标路径文件夹,如果已经存在,则考虑不创建它们。 You need also to make sure that, the destination folder is writable. 您还需要确保目标文件夹是可写的。 use "docroot" for referring to the root folder on your server. 使用“ docroot”来引用服务器上的根文件夹。 You can use something like this 你可以用这样的东西

        if($_FILES )
    {
        $user_id=$_GET['user_id'];
        $device_id=$_GET['device_id'];
        $backup_id=$_GET['backup_id'];
        $images_folder='images';
        $target_path=DOCROOT.$user_id.'\\'.$device_id.'\\'.$backup_id.'\\'.$images_folder.'\\';
        if ( ! is_dir($target_path) OR ! is_writable($target_path))
        {
            mkdir($target_path, 0777, TRUE);
        }
        $target_path = $target_path .$_FILES['uploadedfile']['size']. basename( $_FILES['uploadedfile']['name']);
        if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
        {
            $image_path=$target_path;
            $image_size=$_FILES['uploadedfile']['size'];
            $image = ORM::factory('image');
            $image=$image->new_image($_GET['backup_id'],$image_path,$image_size,$_GET['mobile_path'],file_get_contents($_FILES['uploadedthumb']['tmp_name']));
            echo "Server: Image: ".basename( $_FILES['uploadedfile']['name']).", uploaded successfully";
        } 
        else
        {
            echo "Server: Error";
        }

    }
    else
    {
        echo "no file attached";
    }

I tested your code and had to add a few things to make it work, which will most likely work for you as well. 我测试了您的代码,并且不得不添加一些东西来使其正常工作,这也很可能对您也有效。

method="post" was missing in: method="post"在以下位置丢失:

<form action="action.php" enctype="multipart/form-data" class="dropzone" style="float:left; background-image: url(design.png); ">
  <div class="fallback">
     <input name="file" type="file" multiple />
  </div>
</form>

which is required when uploading files. 上传文件时需要。

Also, missing square brackets for name="file" which should read as name="file[]" - It didn't work for me till I added it, because of the foreach argument. 另外,缺少name="file"方括号,该方括号应读为name="file[]" -由于foreach参数,在我添加它之前,它对我不起作用。

foreach ($_FILES['file']['tmp_name'] as $key => $error)

The brackets are required when using a foreach and multiple files, so that it's treated as an array. 使用foreach和多个文件时,必须使用方括号,以便将其视为数组。

Your form should now read as: 您的表格现在应显示为:

<form action="action.php" method="post" enctype="multipart/form-data" class="dropzone" style="float:left; background-image: url(design.png); ">
  <div class="fallback">
     <input name="file[]" type="file" multiple />
     <input type="submit" name="submit" value="Upload">
  </div>
</form>

I added <input type="submit" name="submit" value="Upload"> since I didn't see it in your posted code. 我添加了<input type="submit" name="submit" value="Upload">因为在您发布的代码中没有看到它。

You also could change $storeFolder = '..\\uploads'; 您还可以更改$storeFolder = '..\\uploads'; to $storeFolder = '../uploads'; $storeFolder = '../uploads'; with a / instead of \\ but that might not make a difference, yet on some systems it does; /代替\\但这可能没有什么区别,但是在某些系统上却可以。 I've seen it happen before. 我以前见过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM