The following form is on my page html page
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
This is the script for the php
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
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("pics/2012/Blackhall Primary/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"pics/2012/Blackhall Primary/" . $_FILES["file"]["name"]);
echo "Stored in: " . "pics/2012/Blackhall Primary/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
When I press select on the form, nothing. It was working fine. The page with the form is called upload pictuture.html and the scriptt is called upload_file.php they are both on the root and the folder pics/2012/blackhall primary does exist and the pics dir is on the root also.
This was working but is not, can anyone see any errors I've made.
Thanks
Ross
Change your privileges and by whom script is executed (usually www-data or apache2) - this account must has write and probably read access to directory, where pictures are uploaded.
Anyway, checking file type based on $_FILES["file"]["type"] isn't safe. This can be easily falsified by hacker.
Related
upload_file.php
$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma", "MP4");
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if ((($_FILES["file"]["type"] == "video/mp4")
|| ($_FILES["file"]["type"] == "audio/mp3")
|| ($_FILES["file"]["type"] == "audio/wma")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))
&& ($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
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"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
html
<!DOCTYPE html>
<head>
<title></title>
</head>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file"><span>Filename:</span></label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
So my problem is I get the message "Invalid File". This only happens when I try to upload video type files. However when I try to upload an image it works like a charm. I've searched all over stackoverflow for other video file upload codes and still couldn't find any that worked. Anyone that could refer another question/solution to me and/or fix this problem will greatly be appreciated.
EXTRA NOTE
I've already tried adding echo "Its type is " . $_FILES["file"]["type"]; to debug what file type is being given however it just returns a nice white space.
Change this part
else
{
echo "Invalid file";
}
to
else
{
echo "Invalid file";
echo "Its type is " . $_FILES["file"]["type"];
}
Now upload the files that don't work and add those types to your list
Apparently the problem only was because the php.ini was set to only accept 10M the file I was uploading was over 15MB and so I guess it gave me the error. But shouldn't that give me the error file-size is too much or something? But that's basically the reason I got the error. :)
Im trying to get a form to upload mp3 or certain image files to a folder.
Using below code I can upload the image files ok, but when i try to upload an mp3 i get the error Undefined index: file in C:\wamp\www\recordlabel\inc\soundclips.php on line 6 and the code echos invalid file from my else block. can anyone offer assistance?
$allowedExts = array("mp3", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "audio/mp3")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
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("../soundclips/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"../soundclips/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../soundclips/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
And this is my form
<form class='form1' action='../inc/soundclips.php' method='post'
enctype='multipart/form-data'>
<input type="hidden" name="id" value="<?php echo $pid; ?>"/>
<b>Add Soundclip For <i><?php echo $e;?></i> </b>
<?php echo"<divclass='editimage'>";
echo "<img class='resizedimage' src='{$row['image']}' />";
echo"</div>";?><br />
<b>Song</b><br /><input type=text size='60' name='asong' /><br />
<input name='file' type="file" id="file" /><br />
<input type='submit' name='add' value='Add Soundclip' />
</form>
Looks like your mp3 file cannot be uploaded, so it is missing in $_FILES array. That might be due to its size compared to image files.
Please check upload_max_filesize and post_max_size settings from your php.ini and allow a greater size than your mp3 file.
Following i give 2 file one is html file which containes file control and submit button for upload medis like image,audio,video etc.I can upload image using following code but when i used for upload audio files its not working.
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file"><span>Filename:</span></label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if ((($_FILES["file"]["type"] == "video/mp4")
|| ($_FILES["file"]["type"] == "audio/mp3")
|| ($_FILES["file"]["type"] == "audio/wma")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))
&& ($_FILES["file"]["size"] < 2000000000000)
&& in_array($extension, $allowedExts))
{
echo "hello";
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
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"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Using above code image uploaded successfully but audio file cannot be uploaded.
Thanks in advance
From the code that you posted, the only error comes from not closing your FORM tag. Other than that, it looks pretty solid.
I have two simple starter scripts (html and php) to choose a file from the machine and then upload to server. However when I press upload, it appears to be uploading the actual php script that deals with the upload. here they are:
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
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"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
THis i'd imagine is just a silly mistake somewhere on my part, but this is my first experience in uploading a file using php. thanks very much.
I used the codes(html+php script) below to upload image file
submit.html
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
php upload file upload_file.php
<?php
echo $_FILES["file"]["type"] ;
echo "<br>";
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
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"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
but it reported error:
image/jpeg
Upload: 02.jpg
Type: image/jpeg
Size: 54.2626953125 Kb
Temp file: /tmp/phpT617qx
Warning: move_uploaded_file(upload/02.jpg): failed to open stream: No such file or directory in /home/virtual/site18/fst/var/www/html/test/upload_file.php on line 29
Warning: move_uploaded_file(): Unable to move '/tmp/phpT617qx' to 'upload/02.jpg' in /home/virtual/site18/fst/var/www/html/test/upload_file.php on line 29
Stored in: upload/02.jpg
submit.html and upload_file.php are in same directory with mod 777
Welcome any comment
It could be because you should assign correct permissions for writing files into that file.
What are the permissions on upload/? Does the directory exist?