PHP Can't find tmp directory

ⅰ亾dé卋堺 提交于 2019-12-05 08:29:00

TMP, TEMP (and maybe TMPDIR) are valid on Windows only and usually pointing to C:\Windows\TEMP. On Linux default temp location is /tmp. To workaround this (works with tempnam() function) you can create a temp folder somewhere within your website space, specify appropriate access permissions and pass this as first parameter to the above function.

Not a great solution but better than nothing.

you can set the upload temp dir in your php.ini - something like that should work:

upload_tmp_dir=/your-www/tmp/

Also, in case you can't edit the php.ini or don't want to do it globally you can use this in the beginning of your script:

ini_set('upload_tmp_dir','/your-home-www/tmp/');

I am running Ubuntu 18.04 and I could create/modify files in the /tmp directory when I ran the PHP script from the CLI, but when I tried accessing the same script as a web page, I could never find the file that was being created. It turns out that Apache by default will create a private tmp directory. The following post provided some insight on the problem Odd Bits - Private /tmp directory. However, the /usr/lib/systemd directory mentioned in the post did not contain any services for http or apache2 on my machine. To help track down the problem I executed the following command:

sudo find / -mount -type f -exec grep -e "PrivateTmp" '{}' ';' -print

and found in /lib/systemd/system/apache2.service the PrivateTmp=true mentioned in the Odd Bits post. Copying the file from /lib/systemd/system to /etc/systemd/system/ and changing true to false and executing

systemctl daemon-restart
systemctl restart apache2

fixed the problem. A person wiser than me suggested copying the file to /etc instead of editing it in /lib was the correct course of action because /lib is 'owned' by the packages and local edits should be performed in /etc. systemd man page describes the systemd configuration processing in gory details.

Probably not the cleanest but this works on my old 5.1.6 install:

function get_temp_path() {
  static $path = null;
  if ($path !== null) return $path;
  $file = tmpfile();
  $meta = stream_get_meta_data($file);
  fclose($file);
  $path = dirname($meta['uri']);
  return $path;
}
Jean Louis

I have the same problem and the solution is to change the apache configuration to expose the TEMP variable to PHP, see this post.

Tip for newbies like me: I THOUGHT that PHP couldn't move stuff from my temporary folder, but I was just confused because of the relative positions of folders. This may apply to someone else, so I'll explain, even though it's very tangentially related to this specific question (because this specific question is a likely search result for other people like me who are newbies).

My problem is that I was echoing an upload form FROM a functions.php file inside of /var/www/html/ TO a profile.php file in /var/www/html/user/ which CALLED an uploadphoto.php file in /var/www/html/. Uploaded files were ultimately intended to land in /var/www/html/uploads. This ultimately meant that most of my references to both uploadphoto.php AND uploads/ in functions.php were written "../uploadphoto.php" or "../uploads/[etc.jpg]", respectively, in order to step back into html/ from html/user/ (where the echoed code landed in html/user/profile.php). This led me to intuitively use the following command inside of uploadphoto.php without thinking it through:

move_uploaded_file($_FILES["file"]["tmp_name"][0], "../uploads/$filename")

See the problem? uploadphoto.php is in the same directory as uploads/, so I did not need the ../ here. For hours, I was sure I was messing up my folder permissions again, because I am new to image uploading. I had forgotten to check for more simple-minded errors. Hopefully this helps someone else.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!