Jump to content

Simple Image Uploader Gone Wrong


wildbuddha

Recommended Posts

Hi everyone.  Thanks, in advance, for any and all help!  I'm sure there's something simple I'm missing as I'm very new to PHP.

function uploadAttachment()
{
	if(isset($_POST['submitAttachment']))
	{
		if ($File)    
		{
    		copy ($File, "$File_name");
			$db_hostname = "localhost"; 
			$db_username = "root";
			$db_password = "";
			$db_name =     "justinalba_module3_db";
			$dbc = @mysqli_connect($db_hostname, $db_username, $db_password, $db_name)
   	 		or die("Unable to connect to MySQL");
			$username = $_POST['userName'];
			$id=$_POST['id'];
			$sql="UPDATE user SET avatarURL='$File_name' WHERE name_username='$username' AND id='$id'";
			$query=mysqli_query($dbc,$sql) or die();
			$sql2="SELECT * FROM user WHERE name_username='$username' AND id='$id'";
			$query2=mysqli_query($dbc,$sql2) or die();
			if($query)
			{
				
				echo "You uploaded the following photo:";
				echo "<img src=\"";
				echo $File_name;
				echo "\" width=\"400px\">";
				unlink ($File);
			}
			else
				{
					echo "You did not upload a file.";
				}
		}
		
	}
}

 

The above code is giving me an error:

"Notice: Undefined variable: File in /Users/justinalba/Sites/php-class/module3/functions.php on line 257"

 

How come the variable can be found on line 255 but not 257?  Do I need to declare $File as something?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/276440-simple-image-uploader-gone-wrong/
Share on other sites

Line 257 in your code above is this

 

{

 

Which is between the two lines that reference $File. Are you sure the error is not on the first instance? Try putting in some echo statements to verify

 

 

echo "Before the if()<br>\n";
if ($File)
{
    echo "After the if()<br>\n";
   copy ($File, "$File_name");
   echo "After the copy()<br>\n";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.