简体   繁体   English

PHP中的文件上传问题

[英]file upload issue in php

Ok I am to the point of pulling my hair out. 好吧,我要拔头发了。 I have been trying all day to get this to work right and nothing. 我整天都在努力使它正常工作,但一无所获。 Right now what this script is doing is when you go to upload the file it goes to the page and there is no image. 现在,此脚本正在执行的操作是,当您上载文件时,该文件将转到页面,并且没有图像。 I have no clue why aint it working right. 我不知道为什么它不能正常工作。 I am still new at this file/image upload thing. 我在此文件/图像上传方面还是个新手。 I have tried a few different ways and nothing. 我尝试了几种不同的方法,但没有尝试。 Here is the upload.php code: 这是upload.php代码:

      <?php
    function dbConnect(){
    // Connect to the database
    $hostname="localhost";
     $database="myDatabase";
     $mysql_login="myLogin";
     $mysql_password="myPassword";

     if(!($db=mysql_connect($hostname, $mysql_login, $mysql_password))){
        echo"error on connect";
     }
     else{
        if(!(mysql_select_db($database,$db))){
            echo mysql_error();
            echo "<br />error on database connection. Check your settings.";
        }
        else{
                    echo "This is the home page. I have successfully made a connection to my database and everything
     is working as it should.";
            }
   }
    $aryImages=array("image/jpeg","image/png");
    $aryDocs=array("application/msword","application/pdf","video/x-msvideo");
    $filename=filenameSafe($_FILES['upload']['name']);
    $fileType=$_FILES["upload"]["type"];
    if (in_array($_FILES["upload"]["type"],$aryImages)){
        createThumb($fileType,$_FILES['upload']['tmp_name'],$filename,100,100);
    }
    elseif (in_array($_FILES["upload"]["type"],$aryDocs)){
        move_uploaded_file($_FILES['upload']['tmp_name'],
    "/home/valerie2/public_html/elinkswap/imagefolder/".$filename);
        $aryColumns=array("sessionID"=>$curSess,"fileName"=>$filename,"fileType"=>$fileType,"thumbFileName"=>$thumbFilename,"dateCreated"=>date('Y-m-d H:i:s'));
        dbInsert($filename,$aryColumns,$_FILES["upload"]["type"]);
    }
    else{

        echo "File Uploaded";
      }
     }
    function createThumb($type,$tmpname,$filename,$new_w,$new_h){
        $thumbFilename="tmb-".$filename;
        echo $type;
        echo "<br>".$tmpname;
        if (is_numeric(strpos($type,"jpeg"))){
            $src_img=imagecreatefromjpeg($tmpname);
        }
        if (is_numeric(strpos($type,"png"))){
            $src_img=imagecreatefrompng($tmpname);
        }
        $old_x=imageSX($src_img);
        $old_y=imageSY($src_img);
        if ($old_x > $old_y) {
            $thumb_w=$new_w;
            $thumb_h=$old_y*($new_h/$old_x);
        }
        if ($old_x < $old_y) {
            $thumb_w=$old_x*($new_w/$old_y);
            $thumb_h=$new_h;
        }
        if ($old_x == $old_y) {
            $thumb_w=$new_w;
            $thumb_h=$new_h;
        }
        $dst_img=imagecreatetruecolor($thumb_w,$thumb_h);
        imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
        if (is_numeric(strpos($type,"jpeg"))){
            imagejpeg($dst_img,"/home/valerie2/public_html/elinkswap/upload/".$thumbFilename);
            imagejpeg($src_img,"/home/valerie2/public_html/elinkswap/upload/".$filename);
        }
        if (is_numeric(strpos($type,"png"))){
            imagepng($dst_img,"/home/valerie2/public_html/elinkswap/upload/".$thumbFilename);
            imagepng($src_img,"/home/valerie2/public_html/elinkswap/upload/".$filename);
        }
        imagedestroy($dst_img);
        imagedestroy($src_img);
        dbInsert($filename,$thumbFilename,$type);
    }
    function filenameSafe($filename) {
        $temp = $filename;
        // Lower case
        $temp = strtolower($temp);
        // Replace spaces with a ’_’
        $temp = str_replace(" ", "_", $temp);
        // Loop through string
        $result = "";
        for ($i=0; $i<strlen($temp); $i++) {
            if (preg_match('([0-9]|[a-z]|_|.)', $temp[$i])) {
                $result = $result.$temp[$i];
            }
        }
        dbConnect();
        $SQL="SELECT fileID FROM upload WHERE fileName='".$result."'";
        //echo $SQL;
        $rs=mysql_query($SQL);
        echo mysql_num_rows($rs);
        if(mysql_num_rows($rs)!=0){
            $extension=strrchr($result,'.');
            $result=str_replace($extension,time(),$result);
            $result=$result.$extension;
        }
        return $result;
    }

    function dbInsert($filename,$thumbFilename,$type){
        dbConnect();
        $SQL="INSERT Into upload (fileName,thumbFileName,fileType) values('".$filename."','".$thumbFilename."','".$type."')";
        //echo $SQL;
        mysql_query($SQL);
    }
    ?>

And this is my index.php code: 这是我的index.php代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>File Upload</title>
<link href="styles.css" type="text/css" rel="stylesheet" />
</head>
<body>


<form enctype="multipart/form-data" action="upload.php" method="post">

Select File: <input type="file" name="upload">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"/>
<input name="Submit" type="submit" value="Upload File">

</form>
</html>

Both these files are inside a folder name imagefolder only with a folder called upload. 这两个文件都在文件夹名称图像文件夹中,只有一个名为上载的文件夹。 I have read so many things on the file/image and so many videos but I still aint understand why the picture wont show up.I even tried to do another way but it kept telling me invaild file type. 我已经阅读了很多关于文件/图像和许多视频的内容,但我仍然不明白为什么图片不会显示,我什至尝试了另一种方法,但是它一直告诉我文件类型不正确。

Your upload.php defines a lot of functions, but are they actually called somewhere? 您的upload.php定义了很多功能,但实际上是在某个地方调用了这些功能吗? Some are calling each other, but none of them seems to be called from "always executed" code. 有些正在互相调用,但似乎没有一个是从“始终执行”的代码中调用的。 You have to add some code which is executed in every case at executing this php file, eg after your dbInsert function: 您必须添加一些在每种情况下都在执行此php文件时执行的代码,例如,在dbInsert函数之后:

function dbInsert($filename,$thumbFilename,$type){
    dbConnect();
    $SQL="INSERT Into upload (fileName,thumbFileName,fileType) values('".$filename."','".$thumbFilename."','".$type."')";
    //echo $SQL;
    mysql_query($SQL);
}
dbConnect();

But from a cursory glance I could't really determine if dbConnect is really the proper function to call - your functions seem a bit randomly interconnected; 但是从粗略的眼光看,我无法真正确定dbConnect是否真的是可以调用的适当函数-您的函数似乎有点随机互连; why is dbConnect calling createThumb , when createThumb is calling dbInsert , which in turn is calling dbConnect again? 为什么dbConnect调用createThumb ,而createThumb调用dbInsert ,而后者又再次调用dbConnect That will create an infinite recursion loop. 这将创建一个无限递归循环。

If I were you I'd start without any functions for testing the wanted behavior. 如果我是您,那么我将开始时没有任何功能来测试所需的行为。 You can always extract functionality to functions later on. 您以后总是可以将功能提取为功能。

似乎有很多功能,但是它们从未被调用,所以我想什么也不会发生。

Check the folder's Permission and simplify your code and try to echo something in each function in order to debug and then determine where does it stop then we can help you more 检查文件夹的权限并简化代码,并尝试在每个函数中回显某些内容以进行调试,然后确定它在哪里停止,然后我们可以为您提供更多帮助

But most likely its a permission issue 但最有可能是权限问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM