在php中上传图片

时间:2009-07-16 00:17:08

标签: php

大家好我想上传图片,但我得到了一个警告,我不明白

这是代码

// print out contents of $_FILES ARRAY

print "Print out of the array of files: FILES <br>";

print_r($_FILES);

print "<br><br>";

$F1 = $_FILES["fname"];

print_r($F1);

print "<br><br>";

// 0 means a successful transfer

  if ($_FILES["fname"]["error"] > 0) {

    print "An error occurred while uploading your file";

    exit(0);
  }

// only accept jpg images pjpeg is for Internet Explorer.. should be jpeg

  if (!($_FILES["fname"]["type"] == "image/pjpeg")) {

    print "I only accept jpg files!";

    exit(0);
  }

// divide size by 1024 to get it in KB

 if ($_FILES["fname"]["size"] / 1024 > 50) {

    print "Your gif file is too large! Less that 50KB please!";

    exit(0);
  }

// check that file is not already there in your uploads folder

  if (file_exists("Uploads/" . $_FILES["fname"]["name"])) {

    print "$F1[name] already exists. Choose another name for your file.";

    exit(0);
  }


// move file from temp location on server to your uploads folder

  **move_uploaded_file($_FILES["fname"]["tmp_name"], "Uploads/".$_FILES["fname"]["name"]);**

  print "Stored in:"." Uploads/".$_FILES["fname"]["name"];


// save location of upload to text file uploads.txt for later use

$datafile = fopen("uploads.txt","a");

flock($datafile,1);

fwrite($datafile, "Uploads/".$_FILES["fname"]["name"]."\n");

flock($datafile,3);

fclose($datafile);

并且警告是(参见粗线)

  

警告:   move_uploaded_file(上传/ avatar3.jpg):   无法打开流:没有这样的文件或   目录   /home/www/mariam.awardspace.info/php/posts.php   在第57行

     

警告:move_uploaded_file():无法使用   将'/ tmp / phprqcpQB'移动到   '上传/ avatar3.jpg'   /home/www/mariam.awardspace.info/php/posts.php   在第57行

提前致谢

2 个答案:

答案 0 :(得分:2)

我想到了两件事。

  1. 尝试使用

    之类的路径

    $ _ SERVER [ 'DOCUMENT_ROOT'] '/路径/到/ file.jpg');

  2. 然后确保上传文件夹存在于您网站的根文件夹中

答案 1 :(得分:1)

目录“上传”不存在,或者您没有足够的权限来写入。