image uploading errors - php

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'])){

Related

How to upload multiple images with php and sql

I can upload one image at a time with my code, however it I have added multiple="multiple" and it lets me highlight more than one image to upload but only one image gets uploaded to the sql database.
Here is my form below -
<form action="upload_image.php" method="post" enctype="multipart/form-data">
<p>Select files:<br /><input type="file" name="image[]" multiple="multiple"/></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>
and my PHP side looks like this -
<?php
if (isset($_FILES['image'], $_POST['album_id'])) {
$image_name = $_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$image_temp = $_FILES['image']['tmp_name'];
$image_dimension = $_FILES['image']['dimension'];
$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 > 10485760){
$errors[] = 'Maximum file size is 10MB';
}
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($image_temp, $image_ext, $album_id);
header('Location: view_album.php?album_id='.$album_id);
exit();
}
}
Any help on this would be amazing!
Regards
Aaron

php image file upload and convert to base64 without saving image

I know how to upload image file and save to other location by using the following code. However, I need to do in such a way that user upload image and automatically convert to base64 without saving that image in my location. How should I do?
<?php
//print_r($_FILES);
if(isset($_FILES['image']))
{
$errors=array();
$allowed_ext= array('jpg','jpeg','png','gif');
$file_name =$_FILES['image']['name'];
// $file_name =$_FILES['image']['tmp_name'];
$file_ext = strtolower( end(explode('.',$file_name)));
$file_size=$_FILES['image']['size'];
$file_tmp= $_FILES['image']['tmp_name'];
echo $file_tmp;echo "<br>";
$type = pathinfo($file_tmp, PATHINFO_EXTENSION);
$data = file_get_contents($file_ext);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
echo "Base64 is ".$base64;
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, 'images/'.$file_name));
{
echo 'File uploaded';
}
}
else
{
foreach($errors as $error)
{
echo $error , '<br/>';
}
}
// print_r($errors);
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<p>
<input type="file" name="image" />
<input type="submit" value="Upload">
</p>
</form>
There is a mistake in your code:
$data = file_get_contents( $file_ext );
This should be:
$data = file_get_contents( $file_tmp );
This should solve your problem.

passing variable value php

there is 2 pages , one is the main and the other included to it
the main page
<?php
$var_value = 7;
$_SESSION['varname'] = $var_value;
include 'upload_image.php';
?>
and the included page
<?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['image_n'], $_POST['image_description'])) {
$image_name = $_FILES['image']['name'];
$bytes = $_FILES['image']['size'];
$image_temp = $_FILES['image']['tmp_name'];
$image_n = $_POST['image_n'];
$image_description = $_POST['image_description'];
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif', 'rar', 'pdf');
//$image_ext = strtolower(end(explode('.', $image_name)));
$image_ext = pathinfo($image_name, PATHINFO_EXTENSION);
$album_id = $_SESSION['varname'];
$errors = array();
if (empty($image_name) || empty($album_id) || empty($image_n) || empty($image_description)) {
$errors[] = 'Something is missing';
} else {
if (strlen($album_name) > 55 || strlen($album_description) > 255) {
$errors[] = 'One or more fields contains too many characters';
}
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 {
$byte = formatSizeUnits($bytes);
upload_image($image_temp, $image_ext, $album_id, $image_n, $image_description, $byte);
header('Location: view_album.php?album_id='.$album_id);
exit();
}
}
$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">
<div class="choose">
<p>Choose a file:<br /><input type="file" name="image" /></p>
</div>
<div class="des">
<p>Name*:<br /><input type="text" name="image_n" maxlength="55"/></p>
<p>Description*:<br /><textarea name="image_description" rows="6" cols="35" maxlength="255"></textarea></p>
<p><input type="submit" value="Upload" /></p>
</div>
</form>
<div class="foot">
<?php
}
include 'template/footer.php';
?>
</div>
the form at the end of the second page does not load .. but when i delete the first line at the main page $var_value = 7 ; the form at the end load .. i don't know what is the problem or there is other way to set the album value in the main and pass it to the included page
If there are no problems found in $album_id, which is set from $var_value, the included file does:
$byte = formatSizeUnits($bytes);
upload_image($image_temp, $image_ext, $album_id, $image_n, $image_description, $byte);
header('Location: view_album.php?album_id='.$album_id);
exit();
So it never gets to the part that displays the form.
The second code is included? If so than you can just use
$album_id = $var_value
Instead of:
$album_id = $_SESSION['varname'];
in the second peace of code... No need for an session.

PHP success on file upload

I'm sure there is a simple solution that I just can't see.
I have a form for uploading stuff.
When the script completes it uses Header('Location: admin.php?success') and a if($_GET['success']) { echo WOOHOO SUCCESS } type message before the rest of the script is run.
The problem with this is that is you want to upload 2 files at once you can't because the first part of the script is executed and nothing else. I then considered using a boolean value to set true or false and display a message from that but that also failed.
I'd like to be able to upload several files in succession and receive a success message for each one.
Many thanks.
relevant PHP:
if(isset($_GET['success']) && empty($_GET['success'])){
echo '<h2>File Upload Successful! Whoop!</h2>';
} else{
if(empty($_POST) === false){
//check that a file has been uploaded
if(isset($_FILES['myTrainingFile']) && !empty($_FILES['myTrainingFile']['tmp_name'])){
file stuff...
if(in_array($fileExt, $blacklist) === true){
$errors[] = "File type not allowed";
}
}
if(empty($errors) === true){
//run update
move file stuff...
}
}
$comments = htmlentities(trim($_POST['comments']));
$category = htmlentities(trim($_POST['category']));
$training->uploadDocument($fileName, $category, $comments);
header('Location: admin.php?success');
exit();
} else if (empty($errors) === false) {
//header('Location: messageUser.php?msg=' .implode($errors));
echo '<p>' . implode('</p><p>', $errors) . '</p>';
}}
}
?>
You need to loop through the $_FILES super-global array and then upload each file.
Here's a working example to give you a better idea.
<?php
$upload_dir= './uploads';
$num_uploads = 2;
$max_file_size = 51200;
$ini_max = str_replace('M', '', ini_get('upload_max_filesize'));
$upload_max = $ini_max * 1024;
$msg = 'Please select files for uploading';
$messages = array();
if(isset($_FILES['userfile']['tmp_name']))
{
for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++)
{
if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i]))
{
$messages[] = 'No file uploaded';
}
elseif($_FILES['userfile']['size'][$i] > $upload_max)
{
$messages[] = "File size exceeds $upload_max php.ini limit";
}
elseif($_FILES['userfile']['size'][$i] > $max_file_size)
{
$messages[] = "File size exceeds $max_file_size limit";
}
else
{
if(#copy($_FILES['userfile']['tmp_name'][$i],$upload_dir.'/'.$_FILES['userfile']['name'][$i]))
{
$messages[] = $_FILES['userfile']['name'][$i].' uploaded';
}
else
{
$messages[] = 'Uploading '.$_FILES['userfile']['name'][$i].' Failed';
}
}
}
}
?>
<html>
<head>
<title>Multiple File Upload</title>
</head>
<body>
<h3><?php echo $msg; ?></h3>
<p>
<?php
if(sizeof($messages) != 0)
{
foreach($messages as $err)
{
echo $err.'<br />';
}
}
?>
</p>
<form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
<?php
$num = 0;
while($num < $num_uploads)
{
echo '<div><input name="userfile[]" type="file" /></div>';
$num++;
}
?>
<input type="submit" value="Upload" />
</form>
</body>
</html>
Hope this helps!

Error with my upload file script (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 ;)

Categories