2012-02-07 117 views
-2

目前,我有这个脚本,用户(使用一种形式,他们可以上传多达七个图像)可以上传多张图片到一个文件夹和图像的名字我的数据库中,没有任何成功。请帮忙。多文件上传与循环

if (isset($_POST['submit'])) { $ref_49 = $_POST['ref_49']; 
    $name = $_POST['name']; 
    $contact = $_POST['contact']; 
    $email = $_POST['email']; 
    $rent_sell = $_POST['rent_sell']; 
    $heading = $_POST['heading']; 
    $price = $_POST['price']; 
    $limitedtextarea = $_POST['limitedtextarea']; 
    $type = $_POST['type']; 
    $where = $_POST['where']; 
    $address = $_POST['address']; 
    $bedroom = $_POST['bedroom']; 
    $bathroom = $_POST['bathroom']; 
    $garages = $_POST['garages']; 
    $carports = $_POST['carports']; 
    $granny_flat = $_POST['granny_flat']; 
    $ref_99 = $_POST['ref_99']; 
    $fulldesc = $_POST['full_desc']; 

    if ($ref_99=="") { 
    $full_ad = "yes"; 
    } else { 
    $full_ad = "no"; 
    } 
    $todays_date = date("Y-m-d"); 
    mkdir("gallery/" . $_POST["name"], 0777); 

for ($i = 0; $i < 7; $i++) 
{ 
    $file_name = $_FILES['uploadFile' . $i]['name']; 
    // strip file_name of slashes 
    $file_name = stripslashes($file_name); 
    $file_name = str_replace("'", "", $file_name); 
    // $copy = copy($_FILES['uploadFile'. $i]['tmp_name'], "gallery/" . $_POST["name"] . "/" . $file_name); 

    if ((($_FILES['uploadFile' . $i]["type"] == "image/gif") 
     || ($_FILES['uploadFile' . $i]["type"] == "image/jpeg") 
     || ($_FILES['uploadFile' . $i]["type"] == "image/pjpeg")) 
     && ($_FILES['uploadFile' . $i]["size"] < 200000000)) 
    { 
     if ($_FILES['uploadFile' . $i]["error"] > 0) 
     { 
      $message = "Return Code: " . $_FILES['uploadFile' . $i]["error"] . "<br />"; 
     } 
     else 
     { 
      $query = "INSERT INTO property (

        name, contact, email, type_of_listing, rent_sell, address, prop_desc, area, price, main_image, image_1, image_2, image_3, image_4, image_5, image_6, heading, bathroom, bedroom, garages, carports, granny_flat, full_description, full_ad, 49_ref, 99_ref, listed 

       ) VALUES (

        '{$name}', '{$contact}', '{$email}', '{$type}', '{$rent_sell}', '{$address}', '{$limitedtextarea}', '{$where}', '{$price}', '{$photo_1}', '{$photo_2}', '{$photo_3}', '{$photo_4}', '{$photo_5}', '{$photo_6}', '{$photo_7}', '{$heading}', '{$bathroom}', '{$bedroom}', '{$garages}', '{$carports}', '{$granny_flat}', '{$fulldesc}', '{$full_ad}', 'ref_49_{$ref_49}', 'ref_99_{$ref_99}', '' 
       )"; 
      $result = mysql_query($query, $connection); 

      if (file_exists("gallery/" . $_POST["name"] . "/" . $_FILES['uploadFile' . $i]["name"])) 
      { 
       $message = "<h3>" . $_FILES['uploadFile' . $i]["name"] . " already exists.</h3>"; 
      } 
      else 
      { 
       move_uploaded_file($_FILES['uploadFile' . $i]["tmp_name"], "gallery/" . $_POST["name"] . "/" . $_FILES['uploadFile' . $i]["name"]); 
       $message = "File: " . $_FILES['uploadFile' . $i]["name"] . " uploaded."; 
      } 
     } 
    } 
    else 
    { 
     $message = "<h3>Invalid file or no file selected.</h3><br />• Only JPEG OR GIF allowed.<br />• Size limited may not exceed 200KB.<br /><a href = \"local_artist.php\">Return</a>"; 
    } 
} 
} 

} 
+3

一个好的开始就是解释这个问题。错误讯息?什么不起作用? – Steve 2012-02-07 08:30:49

+0

对不起,如果我上传所有的七个文件,它会正常工作,但只要我决定只上传一个或两个文件,我就会收到$消息,告诉我没有选择任何文件 – user1002749 2012-02-09 13:15:50

回答

0

这里可能会出现很多问题。你有没有试图把它分解成碎片?你确定数据库连接吗?你确定php有权写入它试图写入的目录吗?你确定那些目录存在...等等。等

注释掉绝大多数的代码,并开始测试全部由一块一块的部件,或包裹在try/catch语句的东西,看看产生了什么错误。

如果只有当您上传< 7个文件时才会出现问题,那么问题在于您已将硬编码到您的循环中!

循环播放实际上传的文件数量,不是固定的数字。

假设它们都是按顺序命名的(并且从0开始),你可以测试循环中散列FILE值的存在性,并继续摄取,直到它变为空(可能是添加限制器来制作肯定不能一直持续下去)

这样的事情...

[编辑2]修改的条件,包括对文件大小的测试

for($i=0; $_FILES['uploadFile' . $i] && $_FILES['uploadFile' . $i]['size'] > 0 && $i<100 ; $i++){ 
    try{ 
    //do your upload stuff here 
    }catch(e){} 
} 

[编辑] 要修改您的页面以包含动态编号场的R请勿这一点:

检查出这个小提琴:http://jsfiddle.net/RjcHY/2/

点击加号和减号按钮在右侧,看看它是如何工作的。我这样做是为了按照你的php的期望命名文件按钮。

+0

Dr.Dredel,是的,我的数据库连接正常,我编辑了代码,并把整个脚本放在里面,这样你就可以看到我做了什么,就像我说的,当我上传全部7张图片时它工作正常,但是当我决定只上传两张或三,我的$信息显示没有选择文件。 – user1002749 2012-02-09 13:20:43

+0

编辑为你增加的乐趣:) – 2012-02-09 16:51:59

+0

哈哈,好的谢谢,但如果我循环到100,那么我必须有一个100文件上传字段?我试过你的代码只是将100更改为6,以便在我的表单中只有7个文件上传字段的accomadate,它的工作原理,但我坐在问题中,我必须填写所有上传字段,否则我会收到消息“Invalid file or no file selected “,也许是每个循环,但不确定如何实现代码。 – user1002749 2012-02-10 11:01:13

0

在处理像文件上传共同任务,写一些库来处理这些任务和需要的地方调用必要的功能。如果您创建上传器类文件,则可以简单地调用您创建的用于处理文件上传的方法之一。

在这里,我会给你一个上传类

<?php 

//Save file as Uploader.php 
//File Uploading Class 

class Uploader 
{ 
private $destinationPath; 
private $errorMessage; 
private $extensions; 
private $allowAll; 
private $maxSize; 
private $uploadName; 
private $seqnence; 
public $name='Uploader'; 
public $useTable =false; 

function setDir($path){ 
$this->destinationPath = $path; 
$this->allowAll = false; 
} 

function allowAllFormats(){ 
$this->allowAll = true; 
} 

function setMaxSize($sizeMB){ 
$this->maxSize = $sizeMB * (1024*1024); 
} 

function setExtensions($options){ 
$this->extensions = $options; 
} 

function setSameFileName(){ 
$this->sameFileName = true; 
$this->sameName = true; 
} 
function getExtension($string){ 
$ext = ""; 
try{ 
$parts = explode(".",$string); 
$ext = strtolower($parts[count($parts)-1]); 
}catch(Exception $c){ 
$ext = ""; 
} 
return $ext; 
} 

function setMessage($message){ 
$this->errorMessage = $message; 
} 

function getMessage(){ 
return $this->errorMessage; 
} 

function getUploadName(){ 
return $this->uploadName; 
} 
function setSequence($seq){ 
$this->imageSeq = $seq; 
} 

function getRandom(){ 
return strtotime(date('Y-m-d H:iConfused')).rand(1111,9999).rand(11,99).rand(111,999); 
} 
function sameName($true){ 
$this->sameName = $true; 
} 
function uploadFile($fileBrowse){ 
$result = false; 
$size = $_FILES[$fileBrowse]["size"]; 
$name = $_FILES[$fileBrowse]["name"]; 
$ext = $this->getExtension($name); 
if(!is_dir($this->destinationPath)){ 
$this->setMessage("Destination folder is not a directory "); 
}else if(!is_writable($this->destinationPath)){ 
$this->setMessage("Destination is not writable !"); 
}else if(empty($name)){ 
$this->setMessage("File not selected "); 
}else if($size>$this->maxSize){ 
$this->setMessage("Too large file !"); 
}else if($this->allowAll || (!$this->allowAll && in_array($ext,$this->extensions))){ 

if($this->sameName==false){ 
$this->uploadName = $this->imageSeq."-".substr(md5(rand(1111,9999)),0,8).$this->getRandom().rand(1111,1000).rand(99,9999).".".$ext; 
}else{ 
$this->uploadName= $name; 
} 
if(move_uploaded_file($_FILES[$fileBrowse]["tmp_name"],$this->destinationPath.$this->uploadName)){ 
$result = true; 
}else{ 
$this->setMessage("Upload failed , try later !"); 
} 
}else{ 
$this->setMessage("Invalid file format !"); 
} 
return $result; 
} 

function deleteUploaded(){ 
unlink($this->destinationPath.$this->uploadName); 
} 

} 

?> 

使用Uploader.php

<?php 

$uploader = new Uploader(); 
$uploader->setDir('uploads/images/'); 
$uploader->setExtensions(array('jpg','jpeg','png','gif')); //allowed extensions list// 
$uploader->setMaxSize(.5); //set max file size to be allowed in MB// 

if($uploader->uploadFile('txtFile')){ //txtFile is the filebrowse element name // 
$image = $uploader->getUploadName(); //get uploaded file name, renames on upload// 

}else{//upload failed 
$uploader->getMessage(); //get upload error message 
} 


?> 

处理多个上传,前3张图片上传 重复块如下

<?php 

for($i=1;$i<=3;$i++){ 

    $uploader->setExtensions(array('jpg','jpeg','png','gif')); //allowed extensions list// 
    $uploader->setMaxSize(.5); //set max file size to be allowed in MB// 
    $uploader->setSequence($i); 
    if($uploader->uploadFile('txtFile'.$i)){ //txtFile is the filebrowse element name // 
     $image = $uploader->getUploadName(); //get uploaded file name, renames on upload// 

    }else{//upload failed 
    $uploader->getMessage(); //get upload error message 
    } 

} 

?> 
在上例中,

,文件浏览组件被命名为txtFile1,txtFil e2,txtFile3 希望你能理解我的解释。

+0

谢谢Kiran现在要尝试它,当我使用您的代码我要如何将数据发送到数据库,而上传图片 – user1002749 2012-02-09 13:17:48

+0

$ image = $ uploader-> getUploadName();这一行给你上传的文件名。即$图像。你可以在mysql查询(插入)中使用这个变量。或将所有上传的文件名保存在一个数组中,如$ images [] = $ uploader-> getUploadName();循环访问数组并执行插入查询 – Kiran 2012-02-09 13:39:33

+0

嗨Kiran,尝试了你的代码,但是失败了,我不确定如何处理你的代码,抱歉我真的是初学者,我把文件保存为uploader.php(单独文件),并把在我的原始页面中,你的评论“在处理多个上传,前3个图像上传重复块如下....”的代码,并添加一个包含(uploader.php),但我得到$ uploader是未定义的错误? ?? – user1002749 2012-02-10 11:05:15