Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.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_Mysql - Fatal编程技术网

在php中上载图像

在php中上载图像,php,mysql,Php,Mysql,当我上传图片时,它抛出了一个错误。如何在windows中更改文件夹的权限设置?我的代码有什么问题吗 //This is my path for storing images. (I am using wamp 2.0) $targetPath="../artistImages/"; $image=$_FILES['myimage']; $image['name'] = mysql_real_escape_string($image['name']); $targetPath.=$image['

当我上传图片时,它抛出了一个错误。如何在windows中更改文件夹的权限设置?我的代码有什么问题吗

//This is my path for storing images. (I am using wamp 2.0)
$targetPath="../artistImages/";

$image=$_FILES['myimage'];
$image['name'] = mysql_real_escape_string($image['name']);
$targetPath.=$image['name'];

if(move_uploaded_file($image['tmp_name'],$targetPath)) {
   $insertQuery="INSERT INTO $tbl_name(ArtistImage)VALUES('".$image['name']."')";                       
   $result=mysql_query($insertQuery) or die("Failed to upload image");
}
else {
   echo "Could not upload file. Check read/write persmissions on the directory";
}

您需要做几件事:

1-确保在将$tablename[ArtistImage]插入查询之前,对一个表名白名单进行测试。
如果您不这样做,您仍然可以接受SQL注入,因为转义只适用于值,而不适用于动态注入SQL语句的表名或列名(或其他SQL语法)

$tbl_name = ......
$allowed_tables = array('table1', 'table2');
if (in_array($tbl_name, $allowed_tables)) {
    $query = "......
} else { echo "tablename not allowed"; }
2-在tablename和
值之间添加空格

$insertQuery= "INSERT INTO `$tbl_name` VALUES('".$image['name']."')"; 
见:

一个更简单的答案是,永远不要让用户指定文件应该保存在哪里,或者如何命名(在文件系统上)。
按照@Marc B的建议,只在数据库中存储描述,并使用PK(id)作为文件名

$description = mysql_real_escape_string($_POST['description']);
$query = "INSERT INTO images (description) VALUES ('$description')"
$result = mysql_query($query);
$id = mysql_insert_id; //get the id you just inserted.
$filename = "../fixed_path/".$id.".jpg";
if (!move_uploaded_file($image['tmp_name'], $filename)) 
{ 
   echo "this should never happen" 
}

您正在尝试将文件移动到一个已被
mysql\u real\u escape\u string()
ed的路径,这意味着它可能包含反斜杠,这显然不起作用(至少不是您期望的方式)

试试这个:

//This is my path for storing images. (I am using wamp 2.0)
$basePath = "../artistImages/";

$image = $_FILES['myimage'];
$storagePath = $basePath.$image['name'];

if (!is_dir($basePath)) {
  echo "Base path '$basePath' does not exist";
} else if (file_exists($storagePath)) {
  echo "File '$storagePath' already exists";
} else if (!move_uploaded_file($image['tmp_name'], $storagePath)) {
   echo "Could not move file to '$storagePath'. Check read/write persmissions on the directory";
} else {
   $insertQuery = "INSERT INTO `$tbl_name` (`ArtistImage`) VALUES ('".mysql_real_escape_string($image['name'])."')";
   if (!mysql_query($insertQuery)) die("DB insert failed: ".mysql_error());
}
请注意,上面的代码有许多错误消息,在将代码放入实际使用的任何环境之前,您可能希望删除这些错误消息,或者至少淡化这些错误消息,但这应该有助于您在开发过程中调试代码

编辑


如果您确实需要更改文件夹的权限,那么在不同版本的Windows中执行此操作的方法会有很大差异(实际上,所有2K+版本的权限几乎相同,但在某些情况下,您可能需要更改某些设置以显示正确的对话框)告诉我们你有错误是没有用的。我们需要看到实际的错误消息。另外,“如何在Windows中更改文件夹的权限”不是一个编程问题,我已经投票将其移动到超级用户。它没有任何sql错误。它有一些权限错误。然后告诉我们什么权限错误。这就是我的全部观点。我从来没有提到过SQL。您的脚本允许用户在服务器上Web服务器UID具有写入权限的任何位置涂鸦文件,从而打开服务器进行远程攻击。远程文件名(
['name']
)是用户指定的,很容易伪造。考虑他们上载“…//..…//..…////Windows /Kalnel.dll”的情况。IUSER_INET不太可能对此有权限,但如果有权限,您的系统就没有权限了。假设您的mysql表有一个整数主键,请使用该数字作为文件名。您可以将原始用户文件名存储在数据库中(在解决注入问题之后)。首先插入DB记录,获取记录的ID,执行文件移动。如果其中任何一项失败,请删除db记录(或回滚事务)并向用户抱怨。感谢您的建议:)我会这样做。但是,我的图像不会上传到文件夹中。这与他问的问题完全无关,而且没有迹象表明,
$tbl_name
来自某个外部来源,因此绝对没有理由在白名单中运行它。@meager,我还有很多其他问题/答案,我犯了一个谨慎的错误,一定要投反对票。@Johan也许你应该发布相关的答案。我的工作少了。否决票不是敌对的东西,它们是网站的一部分。不要因为你的答案因为完全正当的理由被否决而被冒犯,这就是否决票的全部意义。@deepz,别担心,我不介意否决票,只要他们不是路过枪击案。米格尔说得有道理,我只是不同意每一个答案都应该针对所问问题的观点。它说:-基本路径“../artistImages/”不存在。该文件夹位于我的网站www目录中。上述脚本在您的
www
目录中的何处?您用来访问脚本的URL是什么?它是:-C:\wamp\www\Online Video Library。我应该把它放在$basePath中吗?你可以,但它确实让它变得不那么便携。。。
artistImages
目录在哪里?是在
C:\wamp\www\Online Video Library\artistImages
还是
C:\wamp\www\artistImages
?顺便说一句,尽量避免在web目录名中使用空格,这会使您的生活更加混乱,在某种程度上,您的应用程序的可移植性也会降低……没错,这就是您的问题所在。您的
$basePath
应该是
artistImages/
,也可以是
/artistImages/
(单点)。通常,双点表示“上一个目录”,单点表示“这个目录”。您在包含images目录的目录中工作,因此不需要升级。也许值得你浏览一下。。。