PHP and APC, File Upload Progress not Cacheing? - php

I've been all over the internet reading up on APC, and it seems like a nifty way to detect file Uploading.
I am, however, having a problem.
I know how to call files and everything using Ajax, and that is what I am planning to do, but for Testing sake, I'm doing something like this.
Ok, so I have 3 files.
form.php
upload.php
status.php
form.php contains:
<input type="hidden" name="APC_UPLOAD_PROGRESS" value="1234" />
<input type="file" id="fileIn" name="file" />
(I am aware that I will need to use a unique ID in APC_UPLOAD_PROGRESS. Again, this is just for testings sake.)
Ok, Now Upload.php has the regular PHP upload script:
$origin = $_FILES['file']['name'];
if(move_uploaded_file(...etc...etc)...
And Status.php uses APC:
$upload = apc_fetch('upload_1234');
if ($upload) {
if ($upload['done'])
$percent = 100;
else if ($upload['total'] == 0)
$percent = 0;
else
$percent = $upload['current'] / $upload['total'] * 100;
echo $percent;
}
Now What I am doing is uploading a file using a regular HTTP method, and using another window to monitor Status.php.
The problem is; Status.php returns nothing!
However, If i write
print_r(apc_fetch('upload_1234'));
into upload.php, it returns the correct array, with all the details etc..
What am I doing wrong?
Thanks.

When this happens, something to check is that your hidden input element with the APC_UPLOAD_PROGRESS key is placed immediately before the file input in your form.
I know the form in the example above does do this, but it's easily missed in a more complicated form layout.

Related

Basic PHP Upload Form Issue

I want my form to check if the file name equals the users name, in principle everything works fine how I tested it, but the problem I'm having is that the spell check includes the format letters of the image, thus giving a non match.
Example:
Username tries to upload a picture which is correctly named Username.png
The system would work fine, but it takes into account the .png as well, thus gives out a non match. Is there a way I could try and exclude the image format name from the if statement? Because that is pretty much the only problem I'm having with my function, I tried renaming myself to Username.png and then the form works great, but in practice nobody will use such a weird username lol. I'm sorry if my question is a bit confusing or too long, hope it is as clear as I think it is. I will include my small snippet of code here:
if ( ($_FILES["fileToUpload"]["name"]) != $ir['username'] )
{
echo "Sorry, your file is not correctly named.";
$uploadOk = 0;
}
I tried adding a .png after the $ir['username'] like so: $ir['username'].png but then the form just gives out a critical error, so I assume this is a bad spot to write it in. Is there a better way I could do this and is there a way at all? Maybe I could make the form not show up the .png for example? I'm unsure how I could handle this at this point, any input would be invaluable. Thank you for your time, have a good day!
Edit: I will include the form itself as well:
<form action='upload.php' method='post' enctype='multipart/form-data'>
Select image to upload:
<input type='file' name='fileToUpload' id='fileToUpload'><br>
<input type='submit' value='Upload Picture' name='submit'>
</form>
There are several ways of doing that.
The easiest is to split your filename on ., remove the last one and glue it again.
$filename = explode('.', $_FILES["fileToUpload"]["name"]);
array_pop($filename);
$filemame = implode('.', $filename);

PHP file upload gives successful error code, but no image appears

For the sake of clarity I'll break this up into easy to navigate and readable sections.
The Issue:
The $_FILES super-global doesn't appear to hold any information after I upload a file. Despite this, I get error code 0 (successful upload) which seems contradictory.
Background:
I've been working on a personal website for a while now, and am working on a post management system. It's been going great so far, but I'm stuck fast trying to upload an image using $_FILES.
I've done this kind of thing many times at school and my job, but there appears to be some sort of issue with my computer, or I'm having a massive stroke of idiocy.
To fix this issue I've already:
Enabled file uploads in php.ini-production for the version I am using (php 7.1.7)
Changed values such as upload_max_filesize and post_max_size to 100m
Inspected the error code PHP is returning, returns error code 0, or UPLOAD_ERR_OK
Checked my own code for any simple mistakes I'm making. I can't see any, but that's usually the case when you have an error.
I'll include a small test page I wrote, and is demonstrating the issue:
text.php:
<?php
if(isset($_POST['submit'])){
echo $_FILES['a']['error'];
if(isset($_FILE['a']['tmp_name'])){
echo "hello";
}
}
?>
<form action = "text.php" method = "POST" enctype="multipart/form-data">
<label for = "a"> Send Image </label>
<input type = "file" name = "a" id = "a">
<input type = "submit" name = "submit" value = "Submit">
</form>
when the form is submitted, I get error code 0 but the hello test statement does not trigger.
Global array is named $_FILES you are checking for isset($_FILE['a']['tmp_name']
And if you want to check for uploaded file try using
if (is_uploaded_file($_FILES['a']['tmp_name'])) {
//file is uploaded
}

Uploading a profile picture and displaying it

I want users to be able to upload a profile picture (which can be .jpg or .png) and I also want this to be displayed on their profile. I have written some code, based on sources I found here, on Stackoverflow and Google. However, it does not seem to work and I can't find my mistake.
This is the html
<form action="account_settings.php" method="POST">
<input type="file" name="profilePicture"><br><br>
<input type="submit" value="Change!">
</form>
This is how to uploaded file will be processed.
<?php
include ('inc/header.inc.php');
if(isset($_FILES["profilePicture"]["tmp_name"]) && isset($_FILES["profilePicture"]["name"])) {
$ext = pathinfo($_FILES['profilePicture']['name'], PATHINFO_EXTENSION);
$name = $_SESSION['user_login'];
$tmp_name = $_FILES["profilePicture"]["tmp_name"];
if($ext == 'png' || $ext == 'jpg') {
if (isset($tmp_name)) {
if(!empty($tmp_name)) {
$location = '../profielfotos/';
$full_name = $name.'.'.$ext;
if(move_uploaded_file($tmp_name, $location.$full_name)) {
echo 'Photo uploaded!';
}
Down here are just some else statements with error reports.
The code below is used to display the image. I have tested it by putting an image in the profile pictures folder and it did display the image. However, there is still a problem. People are allowed to upload .jpg or .png, how can I make the website display the picture (find the profile picture with the right extension).
I have put this code inside the src attribute of the <img>tag.
<?php if ($handle = opendir('profielfotos/')) {
$file = mysql_real_escape_string($_GET['u']);
echo 'profielfotos/'.$file.'.png';
}
closedir($handle);
I hope someone can help, thanks in advance!
ps. this is my first post ever on stack overflow :-D!
Since you are not storing any info about the file uploaded, you just have check which file exists, using he file_exists() method. See here:
http://php.net/manual/en/function.file-exists.php
So your code will become something like this (Not tested):
<?php if ($handle = opendir('profielfotos/')) {
$file = mysql_real_escape_string($_GET['u']);
if (file_exists('profielfotos/'.$file.'.png')) {
echo 'profielfotos/'.$file.'.png';
} else if (file_exists('profielfotos/'.$file.'.jpg')) {
echo 'profielfotos/'.$file.'.jpg';
}
}
closedir($handle);
You need to add the following to your form:
<form action="account_settings.php" method="POST" enctype="multipart/form-data">
Otherwise it won't allow a file upload as it expects only text.
This is totally insecure. Files uploaded by a user shall never ever be stored within the root of the web server.
Instead, put the files somewhere outside of the doc root.
Write a handler, which takes control of he files
check the mime type by checking the content, not the extension
have arbitrary names, not the name from the upload, that might interfer (imagine 5 people uploading a "profile.png")
let the handler deliver the image by an id ("...imagloader?file=4711"),
name of the file (and extension and location) is stored in a database (with the user record?)

get input path to submit to dropbox in php

I have the following;
$FilePath = "c:\user\test\koala.jpg";
$put = $dropbox->putFile($FilePath);
or
$put = $dropbox->putFile("c:\user\test\koala.jpg");
This works great, and uploads the file to dropbox.
However, obviously I can not get the full path ie c:\user\test\koala.jpg through a form input box, due to security restrictions.
Is there a way around this that would work. Where I can just get $FilePath through some form of input without having to submit it as a temp file to my server.
I have put the full code below.
<?php
// #link https://github.com/BenTheDesigner/Dropbox/blob/master/Dropbox/API.php#L122-139
// Require the bootstrap
require_once('bootstrap.php');
// Extend your sript execution time where required
set_time_limit(0);
$put = $dropbox->putFile($FilePath);
// Dump the output
// var_dump($chunked);
Test something like this:
HTML
...
<form type="POST" action="myPhpUploadPage.php" enctype="multipart/form-data">
<input type="file" name="file"/><br/>
<input type="submit" value="send"/>
</form>
...
PHP (myPhpUploadPage.php)
// Require the bootstrap
require_once('bootstrap.php');
// Extend your sript execution time where required
set_time_limit(0);
if(isset($_FILES['file'])){
$FilePay = $_FILES['file']['tmp_name'];
$put = $dropbox->putFile($FilePath);
} else {
echo 'not file selected';
}
How about uploading your files to your server, and after running the script to copy them to your Dropbox account. Then run a script to erase the files in the temporary directory or specific folder.

copy php no error

I have a form with the possibility to upload an image from the computer to a server, but it won't work. I don't get any error message, so that's quite annoying. (First I got permission denied, but that was solved by changing the rights), but now when I submit the form, everything goes normally, but the file isn't copied to the destination folder. (The folder exists: I tried it with file_exist()...)
Here's part of the code:
<form action='/changingfruit/index.php?item=bad' name='form' method='post' enctype='multipart/form-data'>
<tr>
<td><input type='text' name='titel_nl' value="titel nl" /><br/><input type='text' name='titel_fr' value="titel fr"/></td>
<td><input type='file' name='text_nl' id='text_nl' accept="image/*"/><br/><input type='file' name='text_fr' id="test_fr" accept="image/*"/></td>
<td class="vTop"><input type="submit" value="Bewaar"/></td>
</tr>
</form>
Part where the values are being send to the db:
$str_titel_nl = $_POST["titel_nl"];
$str_titel_fr = $_POST["titel_fr"];
$str_text_nl = $_FILES["text_nl"]["name"];
$str_text_fr = $_FILES["text_fr"]["name"];
if(!empty($_FILES["text_nl"]["name"])){
$tmp = $_FILES['text_nl']['tmp_name'] ;
$foto = $_FILES['text_nl']['name'] ;
$copied = copy($tmp, $images_nl.$foto);
unlink($tmp);
}
(of course the above is just a part of the code: but it's this part that wont work:
if(!empty($_FILES["text_nl"]["name"])){
$tmp = $_FILES['text_nl']['tmp_name'] ;
$foto = $_FILES['text_nl']['name'] ;
$copied = copy($tmp, $images_nl.$foto);
unlink($tmp);
}
The code below this part also works fine, so no error, but also no image.
Does someone knows where the problem could be?
Thanks so much in advance!
FOUND THE ANSWER
So it was indeed a permission problem. Everything was 777, but the last folder where the image was put had 755. (/fruits/img/2012/thumb/) the thumb was 755.I just overlooked it. Thanks everyone for the help!
Your upload code is very messy. Instead of using copy you should be using move_uploaded_file, and also validate that it actually worked and then perform whatever actions needed.
I'm also not sure why each of your line is starts with <?php and ends with ?> ?
You can write it all as one block instead, and i think it would also make more sense and would make your code cleaner for sure.
Last thing i would recommend is reading "Handling File Uploads" from the PHP Manual. It might shed some light on the problems you're having.
P.S. Try adding on top ini_set("display_errors","On"); error_reporting(E_ALL); and see if you're getting any error messages.
please have a look on below link.
PHP upload file to web server from form. error message
http://patelmilap.wordpress.com/2012/01/30/php-file-upload/
you can try this
$flag = #copy($temp, $move);
if ( $flag === true )
{
print "Uploaded";
}
I have posted a simple solution for file uploading without worrying about the implementation .
Click to see the thread
image uploading issue in codeigniter 2.1.0
Please read this section
in that $uploader->getMessage(); will return error string related to the upload failure . So you can understand why the uploading failed .
Thanks

Categories