0

我已经在这个图像脚本上工作了太长时间,但我似乎仍然无法让两件事起作用 - 图像类型和错误处理(如果字段为空)。我有这个的代码,但是我尝试添加它的每个地方,它都不起作用。

我的代码:

$error_message="";
$MaxSize = "600000";
if (isset($_POST['btn_update'])){
function createRandomPassword() {
$chars = "abcde!@#%^fghijkmnoABCDEFGHIJKpqrstuvwxyz023456789ABCDEFGHIJKLMNOPQRSTUVWZ!@#%^&";
srand((double)microtime()*10000000);
$i = 0;
$pass = '' ;
while ($i <= 19) {
$num = rand() % 60;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
if ($_FILES['aMyUploads0']['size'] > $MaxSize || $_FILES['aMyUploads1']['size'] > $MaxSize || $_FILES['aMyUploads2']['size'] > $MaxSize)
{
$error_message = "ERROR: File too big!";

}
$aMyUploads = array();
$password = createRandomPassword();
foreach($_FILES as $aFile)
{
$newLocation = 'uploads/'.$password .$aFile["name"];
if(0 === $aFile['error'] && (false !== move_uploaded_file($aFile['tmp_name'], $newLocation)))
{
$aMyUploads[] = $newLocation;
}
else
{
$aMyUploads[] = '';
}
}
$error_message="Journal successfully saved.";
$connection = mysql_connect("localhost", "????", "???");
mysql_select_db("????", $connection);
$insert = "INSERT INTO photos (image1, image2, image3) VALUES
(
' ".$aMyUploads[0]." ',
' ".$aMyUploads[1]." ',
' ".$aMyUploads[2]." '
)";
$add_member = mysql_query($insert) or die(mysql_error());
}

code im trying to add with no luck:



 //ERROR HANDLING CODE:

    if(empty($aMyUploads[0]) || empty($aMyUploads[1]) || empty($aMyUploads[2]))
    {
    $error_message="Please fill in all fields.";
    }
    else
    {
    $error_message="Journal successfully saved.";


//IMAGE TYPE CODE:

$allowed_filetypes = array(".jpg", ".gif", ".jpeg", ".png");

$ext = substr($newLocation, strpos($newLocation,'.'), strlen($newLocation)-1);
if(!in_array($ext,$allowed_filetypes))
{
die('The file you attempted to upload is not allowed.');
}
4

1 回答 1

0

试试这个检查必填字段,插入在检查文件大小的代码之前。

// ERROR HANDLING CODE:
if (empty($_FILES) || empty($_FILES['aMyUploads0']) || empty($_FILES['aMyUploads1']) || empty($_FILES['aMyUploads2']))
{
    // Handle error
}

这用于验证文件类型,插入到您的foreach.

// IMAGE TYPE CODE:

$allowed_filetypes = array("jpg", "gif", "jpeg", "png");

$ext = pathinfo($aFile['name'], PATHINFO_EXTENSION);
if (!in_array($ext, $allowed_filetypes))
{
    // Handle error
}
于 2012-05-22T20:12:48.703 回答