1

我正在尝试将图像上传到我的 FTP 图像文件夹中。我该怎么做呢?

 <html>
<head>
</head>
<body>
    <?php
 if(isset($_REQUEST['submit']))
 {
 $filename=  $_FILES["imgfile"]["name"];
 if ((($_FILES["imgfile"]["type"] == "image/gif")|| ($_FILES["imgfile"]["type"] ==       "image/jpeg") || ($_FILES["imgfile"]["type"] == "image/png")  || ($_FILES["imgfile"]["type"] == "image/pjpeg"))&& ($_FILES["imgfile"]["size"] < 9999999999))
 {
  if(file_exists($_FILES["imgfile"]["name"]))
 {
 echo "File name exists.";
 }
 else
 {
 move_uploaded_file($_FILES["imgfile"]["tmp_name"],"images/$filename");
 echo "Upload Successful . <a href='images/$filename'>Click here</a> to view the         uploaded image";
}
}
 else
 {
 echo "invalid file.";
 }
 }
 else
 {
 ?>
 <form method="post" enctype="multipart/form-data">
 File name:<input type="file" name="imgfile"><br>
 <input type="submit" name="submit" value="upload">
 </form>

4

1 回答 1

0

以下是检查扩展的方法:

if(is_uploaded_file($_FILES['imgfile']['tmp_name']))
{
    $fileExtension = array('jpg','jpeg','gif','png');
    $file_name = pathinfo($_FILES["imgfile"]["name"]);
    $extension = strtolower($file_name['extension']);
    if(!in_array($extension, $fileExtension))
    {
        $error .= "<span class='error'>invalid file format !</span><br>";
    }
}

.

如果没有发现错误,您将如何上传:

$tmp_file = '';       
if(is_uploaded_file($_FILES['imgfile']['tmp_name']))
{
    $file_name = pathinfo($_FILES["imgfile"]["name"]);
    $extension = strtolower($file_name['extension']);
    $tmp_file = substr($file_name['filename'], 0, 50).'_'.time().'.'.$extension;

    $uploaddir = 'user_image/';
    $uploadfile = $uploaddir.$tmp_file;

    move_uploaded_file($_FILES['imgfile']['tmp_name'], $uploadfile);
    //list($width, $height) = getimagesize($uploadfile);

    $fileType = $_FILES['imgfile']['type'];
    $fileSize = $_FILES['imgfile']['size'];
}
于 2013-04-12T10:07:55.317 回答