Jump to content

[SOLVED] Where's the error?


LemonInflux

Recommended Posts

<?
//print_r($_POST);
$folder = $_GET['fol'];
if($_POST["action"] == "Upload Image")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";


$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";

if(empty($error))
{
$newimage = ($f_user .'/'. $folder .'/'. $imagename);
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}

}

?>

<center><form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<?$_SERVER["PHP_SELF"];?>">
<input type="file" name="image_file" size="20">
<br><br><input type="submit" value="Upload File" name="action">
</form></center>
<div align="center">
<div align="center"><br>
    <?
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
?>
  
<?php
$files = array();
$dir = opendir($f_user .'/'. $folder);
while(($file = readdir($dir)) !== false)  
{  
if($file !== '.' && $file !== '..' && !is_dir($file))  
{  
	$files[] = $file;  
}  
}  
closedir($dir);
natcasesort($files);
echo "<ul>\n";
for($i=0; $i<count($files); $i++)  
{
if($files[$i] != "index.php")
echo '<li><a href="'. $f_user .'/'. $folder .'/'. $files[$i] .'">'. $files[$i] .'</a> - <a href=\delfol.php?del='. $files[$i] .'>Delete</a></li>';
}
echo "</ul>\n";
?>

 

Explanation

 

I want this to list a subfolder in the user's directory. It is an image hoster. Before anyone asks, this is completely flat, no MySQL.

 

Basically, the user ($f_user, as defined in-script (not shown)), has a folder called $f_user (their username). within that, they have a folder called $folder (the url comes out like blah.com/blah/folderview.php?fol=foldername, so the code gets the foldername). Now, I want this code to list $folder's contents, and allow the user to upload to $folder. So far, I have the upload form showing, but when a file is uploaded, it isn't uploaded, neither is it shown. Much appreciated if anyone can work this one out. Thanks in advance,

Tom.

 

PS, for any further queries as to what the variables mean, just post.

Link to comment
Share on other sites

Try removing the @ before the line move_uploaded_file, it is an error suppresor. In this case you wanna see what kind of error you get.

 

I have found that any folders used for uploading need to be set to chmod 777. There may be another way but I don't know of it right now. Check permissions and try to get the error message.

 

maybe try this

 

if(move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage)
{
    echo 'Upload Successful';
}
else
{
    $error["result"] = "There was an error moving the uploaded file.";
}

 

I just noticed that you have a syntax error

 

if(empty($error))
{
   $newimage = ($f_user .'/'. $folder .'/'. $imagename);
    //echo $newimage;
    $result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
{ // you forgot this bracket right here.
    $error["result"] = "There was an error moving the uploaded file.";
}

}

 

Nate

Link to comment
Share on other sites

$HTTP_POST_FILES;

 

This is no longer good. I think that for PHP4 and higher it should be $_FILES[];

 

That error will not cause the script to break and fail to upload the file.

 

I don't mean to insult you, but your code is terrible. You set $_FILES to $HTTP_POST_FILES. WHY???? $_FILES is already a superglobal, why set it to a deprecated var?? Your syntax is hard to read and it is missing several }'s also, you don't close your div tags. No wonder you are having such a hard time, your code is just plain bad. Code to standards. HTML has specific standards, so for the sake of the web community, follow them. Make sure your using the correct syntax in PHP

 

Try fixing the syntax. a proper if statement should be

<?php 
if(1 < 3)
{
     echo 'it is less';
} 
?> 

 

Use the proper brackets to close it out, and also get into the habit of indenting the code as it makes it easier to read and find errors.

 

I fixed a few things, try this and tell me if it helps

 

<?
//print_r($_POST);
$folder = $_GET['fol'];
if($_POST["action"] == "Upload Image")
{ 
unset($imagename);

if(!isset($_FILES['image_file']))
{
	$error["image_file"] = "An image was not found.";
}

$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename))
{
	$error["imagename"] = "The name of the image was not found.";
}

if(empty($error))
{
	$newimage = ($f_user .'/'. $folder .'/'. $imagename);
	//echo $newimage;

	if(move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage))
	{
		echo 'File Uploaded successuflly';
	}
	else
	{
		$error["result"] = "There was an error moving the uploaded file.";
	}
}
}

?>
<center>
  <form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<?$_SERVER["PHP_SELF"];?>">
    <input type="file" name="image_file" size="20">
    <br>
    <br>
    <input type="submit" value="Upload File" name="action">
  </form>
</center>
<div align="center">
<div align="center">
<br>
<?
if(is_array($error))
{
foreach($error as $key=>$val)
{
	echo $val;
	echo "<br>\n";
}
}
?>
<?php
$files = array();
$dir = opendir($f_user .'/'. $folder);
while(($file = readdir($dir)) !== false)  
{  
if($file !== '.' && $file !== '..' && !is_dir($file))  
{  
	$files[] = $file;  
}  
}  
closedir($dir);
natcasesort($files);
echo "<ul>\n";
for($i=0; $i<count($files); $i++)  
{
if($files[$i] != "index.php")
echo '<li><a href="'. $f_user .'/'. $folder .'/'. $files[$i] .'">'. $files[$i] .'</a> - <a href=\delfol.php?del='. $files[$i] .'>Delete</a></li>';
}
echo "</ul>\n";
?>

 

Nate

 

 

Link to comment
Share on other sites

Notice: Undefined index: action in e:\domains\r\reflexprojects.net\user\htdocs\rs\upload\folderview.php on line 30 (which is

if($_POST["action"] == "Upload Image")

),

Notice: Undefined variable: error in e:\domains\r\reflexprojects.net\user\htdocs\rs\upload\folderview.php on line 76 (which is

if(is_array($error))

)

Link to comment
Share on other sites

You really shouldn't bump your topic 10 minutes after a post

 

Im not sure on this but either $_POST['action'] has no value or you should use $_POST['action'] instead of $_POST["action"]

 

and the second part is kinda self-explanatory

 

Undefined variable: error

 

~ Chocopi

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.