Hi Friends:
I wanted to upload a pic to a folder and that worked fine. But then I wanted to replace that pic with another based on logged in user so I thought best would be to store the new upload file name in userprofile db and then retrieve that stored filename to display the pic stored in folder. When I added the additional code to store file name into db I get this error:
Success! Picture Uploaded.
Upload: file_151438.gif
Type: image/gif
Size: 22.447265625 Kb
Temp file: D:\Temp\php\php2BD2.tmp
Stored in: upload/file_151438.gif
Notice: Undefined variable: pic in D:\Hosting\7520570\html\india\uploadfile.php on line 49
My working original code:
<?php
session_start();
require_once 'config.php';
ini_set('display_errors', 1); error_reporting(~0);
if ((($_FILES["file"]["type"] == "image/gif")
||($_FILES["file"]["type"] == "image/png")
||($_FILES["file"]["type"] == "image/jpeg")
||($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 716800))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "<h2>"."Success! Picture Uploaded."."</h2>"."<br />";
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file. <br /> Please ensure file extention is gif, jpeg or png and file size <= 700KB. Thanks for your understanding and co-operation.";
}
?>
<a href="profile2.php"><input type="button" value="Upload another file"></a>
</form>
My not working modified code:
<?php
session_start();
require_once 'config.php';
$pic==$_FILES["file"]["name"];
ini_set('display_errors', 1); error_reporting(~0);
if ((($_FILES["file"]["type"] == "image/gif")
||($_FILES["file"]["type"] == "image/png")
||($_FILES["file"]["type"] == "image/jpeg")
||($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 716800))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "<h2>"."Success! Picture Uploaded."."</h2>"."<br />";
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file. <br /> Please ensure file extention is gif, jpeg or png and file size <= 700KB. Thanks for your understanding and co-operation.";
}
if (isset($_SESSION['user'])){
$user=$_SESSION['user'];
$query = ("UPDATE userprofile set picture=$pic WHERE username=$user");}
else {}
?>
<a href="profile2.php"><input type="button" value="Upload another file"></a>
</form>
What 'm I doing wrong? Is there a better way to do this? Please help. Thanks in advance.🙂