Changing Owner/Group ID In PHP - 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.

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.

PHP File Upload - Can't Upload / Debug

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

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

php unlink problems - file exists

I am having trouble with unlink in php... The files in the directory are uploaded with php form.
ls -l of /files/uploads/
total 6976
-rw-r--r-- 1 alex admin 689030 15 Aug 11:40 01805_goneclubbing_1680x1050.jpg
-rwxrwxrwx 1 alex admin 174932 15 Aug 11:52 4vvF60D.tmp.jpg
-rw-r--r-- 1 alex admin 2699554 15 Aug 12:16 example.JPG
php script (cakePHP framework):
$file_path = '/files/uploads/';
$file_name = $file['Upload']['path'];
$classroom_id = $file['Upload']['classroom_id'];
if (unlink($file_path . $file_name)) {
if ($this->Upload->delete($id)) {
$this->setFlash('File deleted');
$this->redirect(array('controller' => 'classrooms', 'action' => 'view', $classroom_id));
}
}
php error:
Warning (2): unlink(/files/uploads/example.JPG) [function.unlink]: No such file or directory [APP/controllers/uploads_controller.php, line 55]
I searched on stackoverflow, tried giving 777 permissions and still not working. I can access the image through the browser at that path.
Thanks for the help!
On any UNIX system, / is your system root. So when you try to access /files, you try to access a folder files located at your system root. I think you want to access /path_to_www/files, so either use a variable which stores your base path or use a relative path.
On the other hand, when you try to access /files from your web browser, it reaches the / of your web directory (or an alias path). So it is totally normal that you can access your image from your browser but not from php.
The error tells you what the problem is - the file $file_path . $file_name doesn't exist. Echo it out - does it equal what you think it should?
You are referencing the storage location cannonically ('relative' to the root directory /) - did you mean to? Is there actually a directory called /files on your box? If there is I would be surprised...
You should make it if (is_file($file_path . $file_name) && unlink($file_path . $file_name)) in case the file has gone missing because of an external cause to get rid of the ugly error message, and probably also do $file_name = ltrim($file['Upload']['path'],'/'); to get rid of any stray leading slashes...
You have an opening slash in your file path which is telling the application to look at root, not from your current location. You should change your path to include a .,so that it is
./files/uploads/example.JPG.
Try echoing getcwd() to make sure you're in the directory you think you are.
ls -l of /files/uploads/You did this with your user account/shell, didn't you?
Maybe the process running php "sees" the file system differently than your user account does (e.g. a chroot environment or something like that).
<?php
$file_path = '/files/uploads/';
$file_name = $file['Upload']['path'];
$classroom_id = $file['Upload']['classroom_id'];
// <-- debug
echo '<pre>files in ', $file_path, ":\n";
foreach( glob($file_path.'*') as $f) {
echo "'", $f, "'\n";
}
echo "----\n</pre>\n";
// debug -->
if (unlink($file_path . $file_name)) {
if ($this->Upload->delete($id)) {
$this->setFlash('File deleted');
$this->redirect(array('controller' => 'classrooms', 'action' => 'view', $classroom_id));
}
}

file_get_contents with HTTP: No such file or directory

I'm trying to execute a straightforward PHP call to load the contents of a web page:
$result = file_get_contents("http://www.google.com");
The result coming back is a strange file not found error:
Warning: file_get_contents(http://www.google.com): failed to open stream: No such file or directory in /var/www/html/test.php on line 5
I have "allow_url_fopen = On" on my php.ini and no .htaccess files that might alter the setting in the directory. Any ideas? I've never had trouble with this function before on different servers.
Apparently the HTTP stream wrapper is not present, which causes this error.
print_r(stream_get_wrappers());
Array
(
[0] => php
[1] => file
[2] => data
[3] => compress.zlib
)
I'm not sure how it was removed or how to restore it, but that would explain it! I've tried stream_wrapper_restore('http') in case it was unregistered somehow, but that has no effect.
I think it is some kind of proxy effect.
Do you use proxy? If this is the case, you must create a stream context with the proxy details.
Did you re-initialize your webserver on changing "allow_url_fopen"?
OR
The user agent "PHP" may be disallowed on the server you are querying.
OR
From the PHP Manual page: Note: If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().
You can do a test of the php.ini settings like this...
if (ini_get('allow_url_fopen') == '1') {
// use fopen() or file_get_contents()
} else {
// use curl or your custom function
}
Not sure, but try:
if(($fp=fopen('http://www.google.com/', 'rb'))!=null)
{
// for php5 and up or use fread for php4
$contents = stream_get_contents($fp);
fclose($fp);
}

Categories