Error with my upload file script (PHP) - php

I have a script:
if(isset($_FILES['image'])){
$errors = array();
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$file_name = $_FILE['image']['name'];
$file_ext = strtolower(end(explode('.', $file_name)));
$file_size = $_FILE['image']['size'];
$file_tmp = $_FILE['image']['tmp_name'];
if(in_array($file_ext, $allowed_ext) === false){
$errors[] = 'Extension not allowed';
}
if($file_size > 2097152){
$errors[] = 'file size must be under 2mb';
}
if(empty($errors)){
if(move_uploaded_file($file_tmp, "../../img/usr/profile/.$file_name")){
echo 'File uploaded';
}
}else{
foreach($errors as $error){
echo $error, '<br>';
}
}
}
And it's not working. It is supposed to upload an image to a certain directory. Here's the html:
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" name="submit" value="Update Info">
</form>
When I click submit, there is an error at the top of the screen saying 'Extension not allowed'. The html and php is in the same file (I just gave you a small snippet.) Does anything look wrong with my code? thanks!

To access the files, you have to use the $_FILES variable.
In your code, you have sometimes used $_FILE, which does not work ;)

Related

When i try to upload files to my web server it doesnt work but if upload them locally it works

I'm trying to upload files to be more precise images from my web-pages to my webserve but it doesn't work. If i do it locally everything works fine but on the Server it doesn't work.
It doesn't give me out any Error message and i don't know why either.
I've tried to give the right permissions and it didn't work it. I tried another way to program it and it didn't work either. It always shows my first else-loop.
<?php
$SBA_ID = $_GET['SBA_ID'];
if (isset($_POST['submit'])) {
$file = $_FILES['my_file'];
print_r($file);
$fileName = $_FILES['my_file']['name'];
$fileTmpName = $_FILES['my_file']['tmp_name'];
$fileSize = $_FILES['my_file']['size'];
$fileError = $_FILES['my_file']['error'];
$fileType = $_FILES['my_file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png', 'pdf');
if(in_array($fileActualExt, $allowed)){
if ($fileError === 0) {
if($fileSize < 1000000){
$fileNameNew = "Auftrag".$SBA_ID.".".$fileActualExt;
$fileDestination = 'AuftragFotos/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
header("refresh:2;url= ../Startseite.php");
}else {
echo "Your File is too big!";
}
}else {
echo "There was an error uploading your file!";
}
}else {
echo "You cannot uplaod files of this Type";
}
}
My Form
<form action="<?php echo"speichern/Fotospeichern.php?SBA_ID=$SBA_ID"?>"
method="POST" enctype="multipart/form-data">
<input type="file" name="my_file"/>
<button type="submit" name="submit">UPLOAD</button>
</form>
I expect the output to be that the Image is uploaded to the "AuftragFotos" Dir but it always shows: "You cannot upload files of this Type" even though i specified that Type of file to uploaded.
You have a problem with string escaping.
Change:
<form action="<?php echo "speichern/Fotospeichern.php?SBA_ID=$SBA_ID"?>"
method="POST" enctype="multipart/form-data">
<input type="file" name="my_file"/>
<button type="submit" name="submit">UPLOAD</button>
</form>
to:
<form action="speichern/Fotospeichern.php?SBA_ID=<?php echo $SBA_ID; ?>"
method="POST" enctype="multipart/form-data">
<input type="file" name="my_file"/>
<button type="submit" name="submit">UPLOAD</button>
</form>

error in validation of php file extension when uploading

I am trying to validate the file when it is uploading, I am using the following code to achieve it, but I am not getting the desired output.
Even though I am uploading the allowed extension types, I am still getting the echo message "You cannot upload files of this type!". which means the If conditionif (in_array($fileActualExt, $allowed, true)) is not getting true,
thanks in Advance for helping me,
<?php
if(isset($_POST['submit'])) {
if (!empty ($_FILES['file'])){
$fileName = $_FILES['file']['name'];
$fileTempName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png');
if (in_array($fileActualExt, $allowed, true)) {
if ($fileError === 0) {
if ($fileSize <8000) {
$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTempName, $fileDestination);
header("location: player_home.php?uploadSucess");
echo "success";
}else{
echo "your file is too big";
}
}else{
echo "There was an Error uploading your file!";
}
}else{
echo "You cannot upload files of this type!";
}
}
}
?>
HTML CODE
<form method="post">
<div class="form-group">
<input type="file" name="file">
</div>
<span class="sml">Maximum size of <span>8.0MB</span> for a file.‏
<br>
Allowed Types : jpg, jpeg, png
</span>
<br>
<button class="btn btn-primary" type="submit" name="submit" style="margin-top:15px;">Save</button>
</form>
You may be tripping with the true option in the in_array() method. Remove that and the condition should work.

Can I print the file name and path when using a PDF uploader?

I have wrote a simple PDF uploader in PHP. It works fine and the PDF uploads fine. However I would like to print the file name and the file path (where it is being saved). On the upload.php form. Is this possible?
upload.php
<form action="../pdf/upload_pdf2.php" method="POST" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="image"/>
<label><?php echo strlen($filename ) > 0 ? $filename : "" ?> will be saved to /public_html/dev/pdf</label>
<p> </p>
<input type="submit" class="upload-button"/>
</form>
upload_pdf2.php
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions = array("jpeg","jpg","png","pdf");
if(in_array($file_ext,$extensions )=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excatly 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"../pdf/".$file_name);
echo "File uploaded sucessfully";
}else{
print_r($errors);
}
}
?>

PHP multiple images upload

I know this question is repeated over and over.. But can't seem to find solution for my problem...
How can I create a form which will allow user to upload xy images at once?
Here's my html code:
<form class="pure-form" action="upload.php" enctype="multipart/form-data" method="post">
<div class="upload">
<a onclick="select_file()" class="pure-button">Choose a Image</a>
<input id="image" type="file" name="image[]" multiple="multiple">
</div>
<!--image preview-->
<img src="" style="display:none">
<input class="pure-button pure-button-primary" type="submit" value="Upload!">
</form>
And here's my php code:
$valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
$max_size = 2048 * 1024; // max file size (200kb)
$path = 'uploads/'; // upload directory
if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
{
if( #is_uploaded_file($_FILES['image']['tmp_name']) )
{
// get uploaded file extension
$ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
// looking for format and size validity
if (in_array($ext, $valid_exts) AND $_FILES['image']['size'] < $max_size)
{
// unique file path
$path = $path . uniqid(). '.' .$ext;
// move uploaded file from temp to uploads directory
if (move_uploaded_file($_FILES['image']['tmp_name'], $path))
{
$status = 'Image uploaded successfully!';
$status = $path;
}
else {
$status = 'Upload Fail: Unknown error occurred!';
}
}
else {
$status = 'Upload Fail: Unsupported file format or It is too large to upload!';
}
}
else {
$status = 'Upload Fail: File not uploaded!';
}
}
else {
$status = 'Bad request!';
}
// echo out json encoded status
echo json_encode(array('status' => $status));
?>
Is it possible to solve this problem with foreach? If so, how should I do that?
Try this (add as many file fields as you want):
<input type="file" name="image[]" /><br />
<input type="file" name="image[]" /><br />
<input type="file" name="image[]" /><br />
<input type="file" name="image[]" />
Php:
$valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
$max_size = 2048 * 1024; // max file size (200kb)
$path = 'uploads/'; // upload directory
if(isset($_FILES['image'])){
for($i=0; $i<count($_FILES['image']['name']); $i++){
if( #is_uploaded_file($_FILES['image']['tmp_name'][$i]) )
{
// get uploaded file extension
$ext = strtolower(pathinfo($_FILES['image']['name'][$i], PATHINFO_EXTENSION));
// looking for format and size validity
if (in_array($ext, $valid_exts) AND $_FILES['image']['size'][$i] < $max_size)
{
// unique file path
$path = $path . uniqid(). '.' .$ext;
// move uploaded file from temp to uploads directory
if (move_uploaded_file($_FILES['image']['tmp_name'][$i], $path))
{
$status = 'Image uploaded successfully!';
$status = $path;
}
else {
$status = 'Upload Fail: Unknown error occurred!';
}
}
else {
$status = 'Upload Fail: Unsupported file format or It is too large to upload!';
}
}
else {
$status = 'Upload Fail: File not uploaded!';
}
echo "<p>$status</p>";
}
}
?>
<form class="pure-form" action="upload.php" enctype="multipart/form-data" method="post">
<div class="upload">
<a onclick="select_file()" class="pure-button">Choose a Image</a>
<input id="image" type="file" name="image[1]" multiple="multiple">
<input id="image" type="file" name="image[2]" multiple="multiple">
</div>
<!--image preview-->
<img src="" style="display:none">
<input class="pure-button pure-button-primary" type="submit" value="Upload!">
</form>
PHP
$valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
$max_size = 2048 * 1024; // max file size (200kb)
$path = 'uploads/'; // upload directory
if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
{
if( #is_uploaded_file($_FILES['image1']['tmp_name']) )
{
// get uploaded file extension
$ext = strtolower(pathinfo($_FILES['image1']['name'], PATHINFO_EXTENSION));
// looking for format and size validity
if (in_array($ext, $valid_exts) AND $_FILES['image1']['size'] < $max_size)
{
// unique file path
$path = $path . uniqid(). '.' .$ext;
// move uploaded file from temp to uploads directory
if (move_uploaded_file($_FILES['image1']['tmp_name'], $path))
if( #is_uploaded_file($_FILES['image2']['tmp_name']) )
{
// get uploaded file extension
$ext = strtolower(pathinfo($_FILES['image2']['name'], PATHINFO_EXTENSION));
// looking for format and size validity
if (in_array($ext, $valid_exts) AND $_FILES['image2']['size'] < $max_size)
{
// unique file path
$path = $path . uniqid(). '.' .$ext;
// move uploaded file from temp to uploads directory
if (move_uploaded_file($_FILES['image2']['tmp_name'], $path))
{
$status = 'Image uploaded successfully!';
$status = $path;
}
else {
$status = 'Upload Fail: Unknown error occurred!';
}
}
else {
$status = 'Upload Fail: Unsupported file format or It is too large to upload!';
}
}
else {
$status = 'Upload Fail: File not uploaded!';
}
}
else {
$status = 'Bad request!';
}
}}
// echo out json encoded status
echo json_encode(array('status' => $status));
?>

image uploading errors

So I have this issue with my errors not showing up when I test to see if they are showing up when they are supposed to. When I select an file, my script is only supposed to accept image files as well as nothing bigger than 2MB. I haven't written the part that actually uploads the images to the database and the albums that I created but regardless, I should get some sort of error instead of just passing anything through..I need help! Thanks in advance!
Here is the file that processes the image and will eventually upload:
<?php
include 'init.php';
if(!logged_in()){
header('Location: index.php');
exit();
}
include 'template/header.php';
?>
<h3>Upload Image</h3>
<?php
if(isset($FILES['image'], $_POST['album_id'])){
$image_name = $_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$image_temp = $_FILES['image']['tmp_name'];
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$image_ext = strtolower(end(explode('.', $image_name)));
$album_id = $_POST['album_id'];
$errors = array();
if (empty($image_name) || empty($album_id)){
$errors[] = 'Something is missing';
} else {
if(in_array($image_ext, $allowed_ext) === false){
$errors[] = 'File type not allowed';
}
if($image_size > 2097152){
$errors[] = 'Maximum file size is 2MB';
}
if(album_check($album_id) === false){
$errors[] = 'Couldn\'t upload to that album';
}
}
if(!empty($errors)){
foreach ($errors as $error){
echo $error, '<br />';
}
} else {
// upload image
}
}
$albums = get_albums();
if(empty($albums)){
echo '<p>You don\'t have any albums. Create an album</p>';
} else {
?>
<form action="" method="post" enctype="multipart/form-data">
<p>Choose a file:<br /><input type="file" name="image" /></p>
<p>
Choose an album:<br />
<select name="album_id">
<?php
foreach ($albums as $album){
echo '<option value="', $album['id'], '">', $album['name'], '</option>';
}
?>
</select>
</p>
<p><input type="submit" value="Upload" /></p>
</form>
<?php
}
include 'template/footer.php';
?>
I think my issue is around my errors but I'm not sure, again any help is appreciated! Thanks!
-TechGuy24
Change if(isset($FILES['image'], $_POST['album_id'])){
To if(isset($_FILES['image'], $_POST['album_id'])){

Categories