上传特定名称长度的文件

时间:2014-02-24 09:18:41

标签: php file upload

我的代码如下所示:

$uploaded=0;
$ext="";

$uploaded_file_name= strtolower($_FILES['uploaded_file']['name']);

$name_file = explode('.', strtolower(basename($_FILES['uploaded_file']['name'])));
$file_name= $name_file[0];
$file_ext=$name_file[1];
$file_length=strlen($file_name);

$i=strlen($file_name);
while ($i++<8) {
    $file_name .= "0";
}
echo "file_name.ext =" . $file_name . "." . $file_ext;

//do we have a file?
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0))
{


   if ((($file_ext == "jpg")||($file_ext == "png")) && ($_FILES["uploaded_file"]["size"] < 100000)&&(($_FILES["uploaded_file"]["type"] == "image/jpeg")||($_FILES["uploaded_file"]["type"] == "image/png")))
   {
   //Determine the path to which we want to save this file
    $newname = "images/backgrounds/" . $file_name . "." . $file_ext;

       if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname)))
       {
       echo "File uploaded successfully!";
       $uploaded=1;
       }
       else
       {
        echo "Error:!";
        print('<p><a href="../../admin.php?">Back</a></p>');
       }
   } else {
   echo "Error: Only .jpg or .png files are allowed less than 100Kb";
   print('<p><a href="../../admin.php">Back</a></p>');
   }
} else {
echo "Error! File is not uploaded!";
print('<p><a href="../../admin.php">Back</a></p>');
}

文件名已更正,如果文件名太短,则填写0。我尝试上传扩展名为.jpg .jpeg .pnp

的文件

Apache向我展示了错误:

[Mon Feb 24 16:51:54.754787 2014] [:error] [pid 2620] [client 127.0.0.1:39685] PHP Warning:  move_uploaded_file(images/backgrounds/on-off00.jpg): failed to open stream: No such file or directory in /var/www/BB/core/functions/admin_background.php on line 49, referer: http://localhost/BB/admin.php
[Mon Feb 24 16:51:54.754813 2014] [:error] [pid 2620] [client 127.0.0.1:39685] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpmbc7kZ' to 'images/backgrounds/on-off00.jpg' in /var/www/BB/core/functions/admin_background.php on line 49, referer: http://localhost/BB/admin.php
[Mon Feb 24 16:51:54.754844 2014] [:error] [pid 2620] [client 127.0.0.1:39685] PHP Notice:  Array to string conversion in /var/www/BB/core/functions/admin_background.php on line 69, referer: http://localhost/BB/admin.php

在/ tmp目录中找不到该文件的原因是什么?

1 个答案:

答案 0 :(得分:0)

$newname = "images/backgrounds/" . $file_name . "." . $file_ext;
This should contain the full path where you are going to save the image.These messages are   related to incorrect path in server. 
Try to check if "images" Or "background" folder exists before uploading. 
 If these exist then check the full path with is_dir() function