Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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 fopen动态文件路径_Php_Fopen - Fatal编程技术网

php fopen动态文件路径

php fopen动态文件路径,php,fopen,Php,Fopen,我试图制作一个简单的脚本,允许用户将图像上传到我的数据库。我对php fopen函数有问题 if ($_FILES["file"]["error"] == 0) { fopen($_FILES["img"]["tmp_name"], 'r'); } 我需要允许用户上传任何图像文件在他的计算机上,所以路径文件是不同的每一次 <form method="POST"> <input type="file" name="img"> <

我试图制作一个简单的脚本,允许用户将图像上传到我的数据库。我对php fopen函数有问题

if ($_FILES["file"]["error"] == 0) {
    fopen($_FILES["img"]["tmp_name"], 'r');
}
我需要允许用户上传任何图像文件在他的计算机上,所以路径文件是不同的每一次

<form method="POST">
        <input type="file" name="img">
        <input type="submit" value="uload">
    </form>
仅当文件与php脚本位于同一目录中时,此操作才有效


因此,我想问,是否有一种方法可以找到文件的存储位置,以便我可以使用fopen函数打开它。

我建议您首先查看有关文件上载的PHP手册:

if ($_FILES["file"]["error"] == 0) {
    fopen($_FILES["img"]["tmp_name"], 'r');
}
因为这样的代码可能会打开您系统中的安全大厅。一般来说,不建议在执行一些检查之前直接执行用户文件,如病毒扫描、图像的二进制签名、尝试调整图像大小进行图像验证等。。从性能角度来看,不建议将它们保存在数据库中。此外,如果您选择上载到目录,其权限应设置为正确

我将在这里复制PHP手册中修改为您案例的示例文件上载代码:

HTML部分:

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="YOUR_Upload_FILE.php" method="POST">
    <!-- Name of input element determines name in $_FILES array -->
    <input name="img" type="file" />
    <input type="submit" value="Send File" />
</form>

PHP部分:

<?php
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['img']['name']);

//At this point you may like to check:
/*
1. Upload Errors in $_FILES
2. Do virus check.
3. Do binary check for the images type
4. Do size check for the image
5. Do actual image resize to confirm it is valid image.
*/

//After everything is safe, you move the temporary file to your permanent store.
echo '<pre>';
if (move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>

我建议您首先查看PHP手册中有关文件上载的内容:

因为这样的代码可能会打开您系统中的安全大厅。一般来说,不建议在执行一些检查之前直接执行用户文件,如病毒扫描、图像的二进制签名、尝试调整图像大小进行图像验证等。。从性能角度来看,不建议将它们保存在数据库中。此外,如果您选择上载到目录,其权限应设置为正确

我将在这里复制PHP手册中修改为您案例的示例文件上载代码:

HTML部分:

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="YOUR_Upload_FILE.php" method="POST">
    <!-- Name of input element determines name in $_FILES array -->
    <input name="img" type="file" />
    <input type="submit" value="Send File" />
</form>

PHP部分:

<?php
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['img']['name']);

//At this point you may like to check:
/*
1. Upload Errors in $_FILES
2. Do virus check.
3. Do binary check for the images type
4. Do size check for the image
5. Do actual image resize to confirm it is valid image.
*/

//After everything is safe, you move the temporary file to your permanent store.
echo '<pre>';
if (move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>


Ok,这说明了一切,非常感谢。Ok,这说明了一切,非常感谢。$\u文件[“img”][“tmp\u name”]已经包含上传文件的绝对路径My bad。我不确定。谢谢。$\u文件[“img”][“tmp\u name”]已包含上载文件My bad的绝对路径。我不确定。谢谢