PHP File Upload - Can't Upload / Debug - php

I'm trying to upload a file but i doesnt work:
Usefull Info: Running IIS Express (with PHP 5.3) - Windows 7 Professional 32 Bits
Code:
move_uploaded_file($_FILES["imagem"]["name"], "/images/" . $_FILES["imagem"]["name"]) or die ("Error:".print_r($_FILES));
It Prints: Array ( [imagem] => Array ( [name] => Chrysanthemum.jpg [type] => image/jpeg [tmp_name] => C:\Windows\Temp\php3D85.tmp [error] => 0 [size] => 879394 ) )
I'm sure the path is correct and i also did chmod() to set permissions but still, doesn't upload.
Any sugestions?

Your destination path should start with a proper path to the images directory (dirname(__FILE__) can help). As it stands, "/images/" . $_FILES["imagem"]["name"] means it'll try to write to C:/images/ (assuming the script is in the C: drive) which probably doesn't exist.

Since its is inside of an array you need to execute the move uploaded file function inside of foreach loop.
foreach($_FILES['imagem'] as $f){
move_uploaded_file($f['tmp_name'], "/images/" . $f["name"]);
}
You might wanna try using my class:
http://code.google.com/p/daves-upload-class/source/browse/upload_class.php

Related

Uploaded file's tmp_name does not exist

I've been wrangling my brain for the last day trying to fix this.
Basically, the temp file set in $_FILES['logo']['tmp_name'] does not exist.
The file is apparently getting uploaded, as shown in the below print_r() of $_FILES:
Array
(
[logo] => Array
(
[name] => Channelcat.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php26YfhY
[error] => 0
[size] => 152142
)
)
The file permissions on my /tmp directory are apparently 777. I suspect that this problem may have to do with the shared hosting it's using though.
And below is basically what I'm trying to do with the file.
$logo = $_FILES['logo'];
if($logo['size'] > (1024000)) {
die('File size is too large.');
}
$path = __DIR__ . '/uploads/'. $logo['name'];
move_uploaded_file($logo['tmp_name'], $path);
I've tried using is_uploaded_file($logo['tmp_name']), which returns false and realpath($logo['tmp_name']) which returns an empty string.
move_uploaded_file doesn't error, but doesn't move the file to the specified directory either.
When you have set some open_basedir variable in the php.ini file, the file upload works, the $_FILE array gets populated and even shows the tmp_name subkey, still files are not written to disk, which results in a puzzling situation, as no further errors are reported being the error subkey set to 0.
Solution is to set the temp folder to some subfolder inside the open_basedir php.ini variable.

How to upload a file to public html folder to the server and save the path to mysql database?

I want to upload my file into public html folder using php code at the time of uploading i want to insert the file path,size,mime type like this. Please any one give me proper solution because totally I wasted nearly one day for this
Thanks in Advance
If your upload field is named file, you can get all the informations from the $_FILES array:
Array
(
[file] => Array
(
[name] => MyFile.txt
[type] => text/plain
[tmp_name] => /tmp/php/php1h4j1o
[error] => UPLOAD_ERR_OK
[size] => 123
)
)
For the path, you set it yourself with move_uploaded_file:
$path = '/files/MyFile.txt';
move_uploaded_file($_FILE['file']['tmp_name'], $path);
Destination dir must be writable and you should check the return value of move_uploaded_files.

Changing Owner/Group ID In PHP

In my client's ftp the old files have 3002 3000 value in owner/group column.
But when I upload a new page it has 3002 3002 and I can't access this file.
When I try to load this new page it displays error:
NetworkError: 500 Internal Server Error
Why this happen?
If there is any php code to change the 3002 3002 to 3002 3000?
How can I change owner/group value ? Can I change the owner/group Id using any php code?
I already used chown() function , but nothing will happen the owner/group column value in ftp still displaying 3002 3002
<?php
// File name and username to use
$file_name= "test.php";
$path = "/home/sites/public_html/login/" . $file_name ;
$user_name = "root";
// Set the user
chown($path, $user_name);
// Check the result
$stat = stat($path);
print_r(posix_getpwuid($stat['uid']));
?>
It will return something like :-
Array
(
[name] => root
[passwd] => x
[uid] => 0
[gid] => 0
[gecos] => root
[dir] => /root
[shell] => /bin/bash
)
For Changing a file's group :-
<?php
$filename = 'file.txt';
$format = "%s's Group ID # %s: %d\n";
printf($format, $filename, date('r'), filegroup($filename));
chgrp($filename, 8);
clearstatcache(); // do not cache filegroup() results
printf($format, $filename, date('r'), filegroup($filename));
?>
Use chown() to change the files' owner.
chgrp() http://www.php.net/manual/en/function.chgrp.php
chown() http://php.net/manual/en/function.chown.php
When i try to load this new page it displays error:
And why are you trying to change the owner and group of the file? Is it at all possible that the error is actually in something else? Try checking the logs to see what the error "500" means. If you've moved servers, the owner and group may not be the only issue, it may be that some PHP modules haven't been compiled, while you're relying on them.
A permissions issue would actually probably yield a 403 Forbidden, not a 500 Internal Server Error. Again, check your logs to find out what is actually going wrong.

PHP: file missing after upload

After an upload, the print_r output for the image field is as follows
Array
(
[name] => foo.png
[type] => image/png
[tmp_name] => /tmp/php63EvNo
[error] => 0
[size] => 19115
)
Since the error is zero, and the filesize is non-zero I assume that the upload is successful.
A subsequent call to move uploaded file fails: move_uploaded_file(...): failed to open stream: Permission denied
Upon inspecting /tmp, the file named in tmp_name is not there.
What causes this behaviour/ how to rectify?
Thanks!
Extra info:
LAMP stack, running PHP5, CakePHP 2.0
The form:
php/ cake code:
echo $form->input('Foo.image', array('type' => 'file'));
html that is rendered:
<input type="file" name="data[Foo][image]" id="FooImage"/>
The temporary uploaded files in /tmp are deleted at the end of the request -- they are not kept around long-term. You have to use move_uploaded_file during the request that received an upload if you care about the file.
Most likely a permissions issue. Also consider using the cakePHP folder variables. The upload component I have looks like this:
$file = $this->request->data['Client']['image'];
$filename = $file['tmp_name'];
$filePath = WWW_ROOT . DS . 'files' . DS . $file['name'];
if(move_uploaded_file($filename, $filePath))
return '/files/'.$file['name']; // saves location of uploaded file

Calling File Functions in PHP with a Handle of a Filename

From 19 Deadly Sins of Software Security;
     The following code is the poster child for the file-access race condition defect. In between the call to access(2) and open(2), the operating system could switch away from your running process and give another process a time slice. In the interveneing time, the file /tmp/splat could be deleted, and then the application crashes.
…
const char *filename="/tmp/splat";
if (access(filename, R_OK)==0) {
int fd=open(filename, O_RDONLY);
handle_file_contents(fd);
close(fd);
}
and
     Again, this code is accessing the file using a filename. The code determines if the file is readable by the effective user of the Perl script and if it is, reads it. This sinful code is similar to the C/C++ code: between the file check and the read, the file may have disappeared.
#!/user/bin/perl
my $file="$ENV{HOME}/.config";
read_config($file) if -r $file;
and finally,
     Use a file handle, not the filename, to verify the file exists and then open it.
$!/ur/bin/perl
my $file="$ENV{HOME}/.config";
if (open(FILE, "< $file")) {
read_config(*FILE) if is_accessible(*FILE);
}
The point is that if you use a filename for each call to a file-related function, the file could be changed, deleted, etc. between calls, particularly on a remote server. It’s better to use a file handle or file descriptor. Unfortunately, the PHP manual seems to indicate that most file functions only work on a string representing the filename and don’t have overloads that can take a handle instead, filesize in particular:
$fn = "somefile.txt"
$fh = fopen($fn);
if ($fh !== FALSE) {
$data = fread($fh, filesize($fn));
}
That’s not good; between the call to fopen and filesize, the file could have been altered. Worse, the file could have been altered between the call to filesize and the meat of fread!
Does anyone know of a way to use PHP file functions, especially filesize with handles instead of filenames?
A lot of the data you're looking for can be accessed with fstat().
The following can be derived from an fstat() call. See stat() for more about the information fstat() returns. From the PHP manual:
Array
(
[dev] => 771
[ino] => 488704
[mode] => 33188
[nlink] => 1
[uid] => 0
[gid] => 0
[rdev] => 0
[size] => 1114
[atime] => 1061067181
[mtime] => 1056136526
[ctime] => 1056136526
[blksize] => 4096
[blocks] => 8
)

Categories