通过HTML表单将图像上传到FTP

时间:2013-04-12 09:58:58

标签: php html

我正在尝试将图片上传到我的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>

    

1 个答案:

答案 0 :(得分: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'];
}