PHP: uploaded filename conflict - php

I made a form to upload a file, it was working, but when I made a few tests there was sometimes where it didn't worked very well, after hours arguing against my own code I notice that the problem was the name of the file I was trying to upload, it had parentheses like blablabla(1).png,
what can I do to avoid this trouble?
Thanks in advance.

You can use javascript regular expression to replace those invalid symbols that can cost error for your uploading file before submitting it.

Related

What does an '#' in a php file string mean?

I am having trouble googling this, so please bear with me.
I have a code example that is about uploading files and there is the assignment of a temporary file to an array like this:
$postparam['file1'] = '#../res/example.pdf';
I would like to know what the effect of the # character is. I know about error suppression with # but i am confused about the use inside the string.
Thanks.
I found the answer: The example uses cURL and that is why the # is required as a prefix to the filename.
#filename
This will make curl load data from the given file (including any newlines), URL-encode that data and pass it on in the POST.
Source

Why the US zip codes which are not in use is accepting even after validating with proper regex in PHP?

I'm validating US zip code values in PHP. From a form I'm getting one zip code value from the end user. If it's not a valid US zip code an error should be thrown. It throws for some values for some it's not even they are invalid US zip codes. Suppose if user enters zip code value 35302 it must throw an error since 353 is not in use. For zip codes which are currently in use and which are not in use you can refer this link
Wikipedia link to all available US zip code values
Now let's come to the regex code I used for validation :
function zip_code($str) {
return (bool) preg_match("/^([0-9]{5})(-[0-9]{4})?$/i", $str);
}
Note : Here the $str contains the zip code value to be validated.
Please help me in correcting the mistake that I'm making in my code.
Thanks in advance.
As vks said, the regex has no idea of what kind of data you're trying to work with.
A regex is for pattern matching, nothing more, nothing less.
You could use a regex to extract the first three digits of the zip code and compare those against whatever data you have for zip codes.
The USPS has an API to look up zip codes. You could call it directly instead of doing a regex (for one thing a regex doesn't guarantee that a specific code is valid, only that the prefix is valid):
https://www.usps.com/business/web-tools-apis/address-information-api.htm

PHPExcel outputs garbled text

Like many others out there i have had my fair share of issues trying to download an Excel file output by PHPExcel.
What happened in my case was whenever I wanted to download a file using
$obj->save('php://output')
i always used to get garbled text in my excel file with a warning saying my file was corrupt. Eventually i resolved the issue. The problem being i had a
require('dbcon.php')
at the top of my php script. I just replaced that with whatever was there inside dbcon.php and it worked fine again.
Though the problem is solved i would really like to know what caused the problem. It would be great if anyone out there could help me out with this one.
Thanks.
If you get that error - you should follow the advice we always give in that situation: you use a text editor to look in the generated file for leading or trailing whitespace, or plaintext error messages - and then in your own scripts for anything that might generate that such as echo statements, blank lines outside ?> <?php, etc.
Another way of testing for this is to save to the filesystem rather than php://output and see if you get the same problem: if that works, then the problem is always something that your own script is sending to php://output as well.
Clearly you had a problem along those lines in your dbcon.php file. This can be as simple as a trailing newline after a closing ?> in the file...
Tanmay.
In situations like your's, there can be couple of reasons for broken output:
in file dbcon.php can be a whitespace before opening or ending php
tag, so that produce some chars to output and can make file broken
(this is reason for using only opening tag in php 5.3+);
maybe file dbcon.php wasn't found by require, so you got error message in otput;
any other errors or notices or warnings in dbcon.php, because presence of global vars from current file..

newb question - forward slashes and back slashes grr

I'm using cakephp. In my users model I allow ppl to upload a pic. Once it uploads ok I save the url of the pic to $this->data['User']['image_url'] and save it. To upload I use the kind advice given here: http://www.jamesfairhurst.co.uk/posts/view/uploading_files_and_images_with_cakephp
After the upload is done I want to resize the photo so I have a thumbnail. I'm using the advice here: http://bakery.cakephp.org/articles/Perkster/2008/04/12/image-resizer-crop
All is ok, except for the value in $this->data['User']['image_url'] looks like files/photos/userimage.jpg
The upload script I'm using seems to be expecting the urls with backslashes rather than forward ones coz when I run it I get the error
getimagesize(C:\xampp\htdocs\MyNewSite\app\webroot\img\files/photos\$image_name) [function.getimagesize]: failed to open stream:
What can I do to fix this?
The slashes are not the problem, the $image_name is. It's impossible to tell without seeing the code, but you probably are using single quotes when assigning the file name, leading to the variable not getting parsed properly.
Windows accepts both directory separators \ and /, thus the problem must be somewhere else. This is taken from the error message
C:\xampp\htdocs\MyNewSite\app\webroot\img\files/photos\$image_name
I assume, that there is no file $image_name ;) It seems, that you didnt let PHP evaluate the variables when calling getimagesize()

strange thing, ajax response includes whitespace

I ave several JS functions that call different php files with $.ajax jquery method... yesterday everything was fine, but after cleaning up my code, i don't know what i did, but now the ajax response is like "[space]data" instead of "data"..
i could use a trim function in Js but i want to fix the source of the problem...
all my php files have the missing last ?> in order to avoid that, and before <?php i'm sure, just checked, there is no space...
how come did I introduce this error? is the server? the browser?
The funny thing is that yesterday i cleaned my code with JSLINT..! bad idea..
thanks
When I had the same problem it was just a carriage return or space after the closing PHP tag, a surprisingly easy thing to introduce by accident.
Make sure you open the PHP tag at the start of the first line of your script, close it at the end and delete everything after the closed tag (should be easy to spot in a good editor).
I can see no reason why not closing your PHP tag wouldn't just be really annoying.. but thats just me!
I know I'm late this this thread, found it because I had the same issue. I was able to confirm that the although you may clear all the spacing in your callback function it is still possible to get these white-space/carriage returns in your response text (you can even see this in chrome developer tools under the "Network" tab).
So I tested and found that if you put ob_clean(); at the top of your callback function then it clears any of those spaces. I don't know much about this function just that it was mentioned in the codex (http://codex.wordpress.org/AJAX_in_Plugins#Debugging). Hope that helps anyone else that find there way here because of the same issue
If your response from the server includes the extra space, then the php code is the cause. If your response does not include the extra space, then the problem must be in javascript.
Use Fiddler to examine the actual response from the server to see if it really has the space in it. You'll then know for sure if the problem is with PHP or JS. Then you can post the relevant code.

Categories