2012-12-28 47 views
0

我的代码:与PHP的问题上传

if(isset($_FILES['image'])){ 
    $allowedExts = array('jpg', 'gif', 'png'); 
    $extension = end(explode('.', $_FILES['image']['name'])); 
    if(in_array($extension, $allowedExts)){ 
     if($_FILES['image']['size'] < 50000){ 
      if ($_FILES['image']['error'] > 0){ 
       $uploaderror = $_FILES['image']['error']; 
      }else{ 
       $uploaderror = 'FALLBACK ERROR'; 
       if(file_exists('..images/'.$_FILES['image']['name'])){ 
        $uploaderror = 'The file <strong>'.$_FILES['image']['name'].'</strong> already exists in the images directory.'; 
       }else{ 
        move_uploaded_file($_FILES['file']['tmp_name'], '..images/'.$_FILES['file']['name']); 
        $uploadsuccess = $_FILES['file']['name']; 
       } 
      } 
     }else{$uploaderror = 'The image is too large.';} 
    }else{$uploaderror = 'Only images (.jpg, .png, and .gif) are allowed.';} 
}else{$uploaderror = 'No attempt';} 



输出: $uploaderror回报FALLBACK ERROR$uploadsuccess没有设置。该文件没有出现在指定的目录中,我无法在服务器上找到它。请告诉我我做错了什么。谢谢!

+2

哇,这是一些嘈杂的代码。 – BastiBen

+1

你的表单是什么样的?在某些地方你使用'$ _FILES ['image']'和其他你使用'$ _FILES ['file']' –

+2

这种格式是错误的:''..images /'.$_ FILES ['file'] ['' name']'你丢失''''''''''''后面的'''' –

回答

1

你失踪后/..images前,要解决这个问题只是改变这一点:

move_uploaded_file($_FILES['file']['tmp_name'], '..images/'.$_FILES['file']['name']); 

以下几点:

move_uploaded_file($_FILES['file']['tmp_name'], '../images/'.$_FILES['file']['name']); 

与您的代码通过一个终端,你可以运行得到以下回应:

..images:没有这样的文件或目录

编辑

我发现了另一个地方,你忘了/,这是你的file_exists()检查。

我已经清理你的代码也使其更具可读性:

<?php 
$errors = array(); 
$allowedExts = array('jpg', 'gif', 'png'); 
$extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); 
if(!isset($_FILES['image'])){ 
    $errors[] = "No attempt."; 
} 
if(in_array($extension, $allowedExts)){ 
    $errors[] = "Only images (.jpg, .png, and .gif) are allowed."; 
} 
if($_FILES['image']['size'] > 50000){ 
    $errors[] = "The image is too large."; 
} 
if ($_FILES['image']['error'] <= 0){ 
    $errors[] = $_FILES['image']['error']; 
} 
if(file_exists('../images/'.$_FILES['image']['name'])){ 
    $errors[] = 'The file <strong>'.$_FILES['image']['name'].'</strong> already exists in the images directory.'; 
} 

// No errors found! 
if(count($errors) == 0){ 
    move_uploaded_file($_FILES['file']['tmp_name'], '../images/'.$_FILES['file']['name']); 
    $uploadsuccess = $_FILES['file']['name']; 
} 
+0

谢谢!使用数组的错误是好的,但不适用于'if(isset($ uploaderror))',我稍后使用它。 – Mooseman

+1

你可以做'if(!empty($ errors))'这意味着它包含错误 –