PHP works in a folder but not in another
Ok, so I have made a form on a .html page and a .php page to upload an image and then save som data from the form along with the filepath for the image. The problem is when I move it from the root folder to root/backweb it wont work. I have created a new uploads folder in the backweb folder and also copied the conn.inc.php file over to the backweb folder for simplicity.
When I run it from the root foler it says "Releasen sparades!" (Release saved) but when I run it from the backweb server it says "Ogiltig filtyp!..." (Invalid filetype). Any ideas?
Here's the code:
<?
require_once 'conn.inc.php';
if (($_FILES["cover"]["type"] == "image/gif")
|| ($_FILES["cover"]["type"] == "image/png")
|| ($_FILES["cover"]["type"] == "image/jpeg")
|| ($_FILES["cover"]["type"] == "image/pjpeg"))
{
if ($_FILES["cover"]["error"] > 0)
{
echo "Return Code: " . $_FILES["cover"]["error"] . "<br />";
}
else
{
if (file_exists("uploads/" . $_FILES["cover"]["name"]))
{
echo $_FILES["cover"]["name"] . " finns redan på servern. ";
}
else
{
move_uploaded_file($_FILES["cover"]["tmp_name"],
"uploads/" . $_FILES["cover"]["name"]);
$cover = "uploads/" . $_FILES["cover"]["name"];
}
}
}
else
{
echo "Ogiltig filtyp! Endast bilder är tillåtna (.jpg, .png och .gif)</br>
<a href=\"uploadform.html\">Försök igen</a>";
}
$band = $_REQUEST["band"];
$title = $_REQUEST["title"];
$catnum = $_REQUEST["catnum"];
$format = $_REQUEST["format"];
$query = "INSERT INTO releases (band,title,catnum,format,cover) VALUES ('$band','$title','$catnum','$format','$cover')";
$result = mysql_query($query);
if ($result == NULL)
{
echo "Något gick fel, releasen sparades ej!";
?>
<br/&g开发者_运维百科t;
<br/>
<a href="editreleases.php">Försök igen!</a>
<?
}
else
{
echo "Releasen sparades!";
}
?>
You have to give correct paths to your directories, ie
if (file_exists("../uploads/" . $_FILES["cover"]["name"]))
vs
if (file_exists("uploads/" . $_FILES["cover"]["name"]))
sound like folder permissions are not set for PHP to access files in the non-root folder
Maybe you should use is_uploaded_file
instead of file_exists
? And use $_FILES["cover"]["tmp_name"]
for the path to the file. Example from PHP manual:
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.\n";
echo "Displaying contents\n";
readfile($_FILES['userfile']['tmp_name']);
}
else
{
echo "Possible file upload attack: ";
echo "filename '". $_FILES['userfile']['tmp_name'] . "'.";
}
In addition, as @trailmax notes, check that PHP can actually access the folder files are uploaded to.
精彩评论