Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 创建图像上载选项的困难_Php_Arrays_Image_File - Fatal编程技术网

Php 创建图像上载选项的困难

Php 创建图像上载选项的困难,php,arrays,image,file,Php,Arrays,Image,File,我创建了一个测试页面来学习如何上传图像。我试图将图像保存到我创建的名为image的文件夹中,然后在我的数据库中存储该图像的文件名,以帮助节省空间。现在,图像文件名不是存储,而是存储单词数组。我确实为此犯了一个错误,其他人也一样。单击“上载”后,出现以下错误: 警告:move_Upload_fileimage/picturetest.jpg:无法打开流:第34行的/home4/fdfsfs/public_html/example.com/img_test.php中没有此类文件或目录 警告:move

我创建了一个测试页面来学习如何上传图像。我试图将图像保存到我创建的名为image的文件夹中,然后在我的数据库中存储该图像的文件名,以帮助节省空间。现在,图像文件名不是存储,而是存储单词数组。我确实为此犯了一个错误,其他人也一样。单击“上载”后,出现以下错误:

警告:move_Upload_fileimage/picturetest.jpg:无法打开流:第34行的/home4/fdfsfs/public_html/example.com/img_test.php中没有此类文件或目录

警告:move_Upload_文件:无法将第34行/home4/fdsfafa/public_html/example.com/img_test.php中的“/tmp/phpUg7p4D”移动到“image/picturetest.jpg”

**注意:第59行/home4/fdsfaf/public_html/example.com/img_test.php中的数组到字符串转换

第34行是:

if (move_uploaded_file($tmp_name, $destinationFolder.$filename)) {
第59行是:

$stmt->execute();
完整脚本:

$filename = $_FILES['file']['name'];
//$filesize = $_FILES['file']['size'];
//$filetype = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
$file_error = $_FILES['file']['error'];

if(isset($_POST['create'])){
    $file = $filename;
    $file = $_FILES['file'];
    //$file = "productpics/". $_FILES['file']['name']; // save the filename
}else {
    echo "error!";
    }
            
if (isset($filename )) {
    if (!empty($filename)) {
                
        $destinationFolder = 'image/';
                
        if (move_uploaded_file($tmp_name, $destinationFolder.$filename)) {
            echo 'Uploaded!';
        } else {
            echo 'There was an error!';
        }
                
    } else {
        echo 'Please choose a file.';
    }
}


//Connection
$con = mysqli_connect("localhost","","","");
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    if ($stmt = mysqli_prepare($con, "INSERT INTO image (img) VALUES (?)")) {

                    /* bind parameters for markers */
                    $stmt->bind_param('s', $file);

                    /* execute query */
                    $stmt->execute();
                    //if(!$stmt->execute()){trigger_error("there was an error....".$con->error, E_USER_WARNING);}

                /* close statement */
                mysqli_stmt_close($stmt);
                    echo "Success!";
                } else {
                    echo "Failed!";
                }
                
$result = mysqli_query($con,"SELECT * FROM image");
    if($row = mysqli_fetch_array($result)) {
        if($row['img'] == ""){
            echo "<img src='images/default_pic.png' alt='No Picture'>";
        } else {
            echo "<img src='images/".$row['img']."' alt='Profile Picture'>";
        }
            echo "<br><br><br><br>";
    }
?>


<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="file" class="inputbarfile">
    <input type="submit" name="create" id="signinButton" value="Upload">
</form>

有人看到我在做什么了吗???

你需要将数组转换成字符串,看看这个


所以,为了重做你所拥有的,我把它分解成了部分函数。基本上是一样的,但是每个部分都被分解了,所以管理每个部分更容易2更容易进行故障排除3更容易添加错误处理

<?php
// If you make a file function, you can change where things are saved
// You can also change the destination (for portability)
function UploadFile($fileArray = array(), $destinationFolder = 'image/')
    {
        $filename       =   $fileArray['file']['name'];
        $tmp_name       =   $fileArray['file']['tmp_name'];
        $filesize       =   $fileArray['file']['size'];
        $file_error     =   $fileArray['file']['error'];
        $file           =   $fileArray['file'];
        // Save all the default data.
        // Success and error should be set by default to fail
        $return['error']        =   true;
        $return['success']      =   false;
        $return['file']['dest'] =   $destinationFolder.$filename;
        $return['file']['size'] =   $filesize;

        if($file_error == 0)
            $return['error']    =   false;
        // I added a directory creation function so you don't have to 
        // manually make folders. This will do it for you.
        if(!is_dir($destinationFolder))
            mkdir($destinationFolder,0755,true);
        // If your filename is not empty, return success or fail of upload
        if (!empty($filename))
            $return['success']  =   (move_uploaded_file($tmp_name, $destinationFolder.$filename));  

        return $return; 
    }

// Create a function that quickly returns your connection   
function Connection()
    {
        //Connection
        $con = mysqli_connect("localhost","","","");
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s\n", mysqli_connect_error());
                exit();
            }

        return $con;
    }

// Create a save-to-database function so it's easier and reusable
function SaveToDb($con,$filename = false)
    {
        // Return fail immediately if the connection is false
        // or the image name is invalid
        if(empty($filename) || !$con)
            return false;

        if ($stmt = mysqli_prepare($con, "INSERT INTO image (img) VALUES (?)")) {
                $stmt->bind_param('s', $filename);
                $stmt->execute();
                mysqli_stmt_close($stmt);

                return true;
            }

        return false;
    }

// This just gets the image from the destination column. Not the most
// efficient, but you can change it to fetch by id or whatever is unique
function getPhoto($con,$dest)
    {
        $result = mysqli_query($con,"SELECT * FROM `image` where `img` = '$dest'");
        if($row = mysqli_fetch_array($result))
            return $row;

        return 0;
    }
使用:


您可能希望将其保存到数据库:$destinationFolder.$FileName对于第一个警告,您确定将图像存储的文件夹设置在正确的位置吗?尝试使用mkdir在顶部创建一个文件夹,分配$file=$\u FILES['file'];将$_FILES['file']数组分配给变量$file。在底部,您尝试在不处理数组的情况下将该数组写入数据库,以使其写入数组警告是指向找到的目录或文件,因此它可能试图保存到不同位置的文件夹中。您可能正在尝试存储最终目标,即:$destinationFolder.$filename。将其放入数据库虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会无效。首先,非常感谢您详细介绍!好的,这带走了我明显得到的信息。这肯定超出了我的技能水平,所以我正在努力把它全部融入其中。每当我上传一张照片时,它都会在页面上以一个非常大的版本显示图像,所以它的真实大小是一样的。是否仍然可以使图像仅显示在“您的图像”框中?如果你愿意,你可以在我的网站上看到它在做什么。我现在只是在玩这个,所以如果有帮助的话,请随意上传图片。realtorcatch.com/test\u test\u imgYes,您可以使用CSS->max width和/或max height使其以正确的大小显示在特定的图像框中,以使其显示在您想要的框中,您只需更改逻辑并将alt=/>放在IFideal之外,您将希望将用户信息保存到会话以填充该字段。我的更改正确吗?要将用户的信息保存到会话中,我是否只需保存图片,以便显示他们当前的图片?您要做的是,当用户登录时,将用户id、用户名,可能还有一些其他变量保存到会话中,然后在将用户图像保存到磁盘时,在数据库中保存一个与他们的图像对应的对他们的引用。然后,当他们转到他们的个人资料时,你可以拉它。
<?php
// If you make a file function, you can change where things are saved
// You can also change the destination (for portability)
function UploadFile($fileArray = array(), $destinationFolder = 'image/')
    {
        $filename       =   $fileArray['file']['name'];
        $tmp_name       =   $fileArray['file']['tmp_name'];
        $filesize       =   $fileArray['file']['size'];
        $file_error     =   $fileArray['file']['error'];
        $file           =   $fileArray['file'];
        // Save all the default data.
        // Success and error should be set by default to fail
        $return['error']        =   true;
        $return['success']      =   false;
        $return['file']['dest'] =   $destinationFolder.$filename;
        $return['file']['size'] =   $filesize;

        if($file_error == 0)
            $return['error']    =   false;
        // I added a directory creation function so you don't have to 
        // manually make folders. This will do it for you.
        if(!is_dir($destinationFolder))
            mkdir($destinationFolder,0755,true);
        // If your filename is not empty, return success or fail of upload
        if (!empty($filename))
            $return['success']  =   (move_uploaded_file($tmp_name, $destinationFolder.$filename));  

        return $return; 
    }

// Create a function that quickly returns your connection   
function Connection()
    {
        //Connection
        $con = mysqli_connect("localhost","","","");
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s\n", mysqli_connect_error());
                exit();
            }

        return $con;
    }

// Create a save-to-database function so it's easier and reusable
function SaveToDb($con,$filename = false)
    {
        // Return fail immediately if the connection is false
        // or the image name is invalid
        if(empty($filename) || !$con)
            return false;

        if ($stmt = mysqli_prepare($con, "INSERT INTO image (img) VALUES (?)")) {
                $stmt->bind_param('s', $filename);
                $stmt->execute();
                mysqli_stmt_close($stmt);

                return true;
            }

        return false;
    }

// This just gets the image from the destination column. Not the most
// efficient, but you can change it to fetch by id or whatever is unique
function getPhoto($con,$dest)
    {
        $result = mysqli_query($con,"SELECT * FROM `image` where `img` = '$dest'");
        if($row = mysqli_fetch_array($result))
            return $row;

        return 0;
    }
// Make sure all functions above are include here

// Get the database connection
$con        =   Connection();
// Check for post   
if(isset($_POST['create'])) {
        // Try uploading
        $upload =   UploadFile($_FILES);
        // If upload fails
        if(!$upload['success'])
            echo '<h3>Sorry, an error occurred</h3>';
        else {
                // You could add error handling here based on the results of 
                // each function's success or failure below.

                // Try to save it
                $saveToDb   =   SaveToDb($con,$upload['file']['dest']);
                // Get the profile from image name
                $profPic    =   ($saveToDb)? getPhoto($con,$upload['file']['dest']) : false; ?>

                <img src="<?php echo (!empty($profPic) && $profPic != 0)? $profPic['img'] : "default_pic.png"; ?>" alt="<?php echo (!empty($profPic) && $profPic != 0)? "Profile Picture" : "No Picture"; ?>" />
                <?php
            }
    }
?>