dongye8110 2016-06-16 12:24
浏览 20

if if语句上传文件

I have a simple file uploader on my index.php page which it gets posted to my addnew.php page along with 2 other input fields like this below:

<form action="addnew.php" method="post" enctype="multipart/form-data">
<input type="text" name="title" placeholder="Title">
<input type="text" name="location" placeholder="location">
<input type="file" name="file">
<input type="submit" value="add">
</form>

my question is that when it sends to the file upload, it sends a generateRandomString function to replace the original file name, so if the form field is empty, it still sends a string of 10 random characters to my database. My question is that how can I set a if/else statement so that it does not send the random characters to my database if the upload field is empty. Here's the idea that I have, but it's obviously not working..

if (!empty($_FILES['file'])=="") {
    $sql="INSERT INTO tabletry (title, location)
          VALUES ('".addslashes($title)."', '".addslashes($location)."')";
} else {
    $ext = pathinfo($_FILES['file']['name'])['extension'];
    $nFn = generateRandomString(). ".$ext";
    move_uploaded_file($_FILES['file']['tmp_name'],"pictures/".$nFn);

    function generateRandomString($legnth = 10) {
        return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"),0, $legnth);
    }

    $sql="INSERT INTO tabletry (title, location, mainimage) 
          VALUES ('".addslashes($title)."', '".addslashes($location)."', '{$nFn}')";  
}

any help would be amazing..

  • 写回答

1条回答 默认 最新

  • dongzouxigu12345 2016-06-16 12:31
    关注

    Don't compare empty() with any value this condition is always false and your code enter into else.

    You need to change

    if (!empty($_FILES['file'])=="") {// wrong use of empty
    

    To

     if (!empty($_FILES['file'])
    

    OR use file upload validation as

     $check = getimagesize($_FILES["file"]["tmp_name"]);
            if($check !== false) {
        /// Your code
    
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法