0

I have current PHP code that works when attaching files on a form. The files have to go to a certain directory. I know that while attaching in Wordpress I need to use a different syntax. However, I've been reading for days literally on how I can do this. Again, my code here works on a standalone PHP page but not in Wordpress. I'm not that worried about security either since my page is on an intranet site behind a firewall and cannot be reached otherwise. I see some tutorials and code for uploading to a custom directory in the WP uploads folder, but I need to upload to a directory in the root of the web server. Here's my code currently from the standalone PHP page.

$folderPath = "/jobtracking/files/standard/{$projID}";
mkdir($folderPath);

$filename = basename($_FILES["Attach_Files"]["name"]);
$filetype = $_FILES["Attach_Files"]["type"];
$datei = "/jobtracking/files/standard/{$projID}/{$filename}";
$target_path = $folderPath . "/" . $filename;
    if(move_uploaded_file($_FILES["Attach_Files"]["tmp_name"], $target_path)) {
    mysql_query("INSERT INTO files(files.name, files.project, files.added, files.datei, files.type, files.folder) VALUES('{$filename}', '{$projID}', UNIX_TIMESTAMP(now()), '{$datei}', '{$filetype}', 0)");
    }
chmod("{$target_path}", 0777);

I've seen the codex on wp_handle_upload but not sure if that applies to my need. I feel like that's what I need to use but having a hard time finding anything that fits what I'm trying to do. I don't need the form attachment to go into Wordpress at all.

I saw this article which seemed promising but it is still uploading to putting files inside the WP directory and not outside.

Also tried to use this article as reference.

My question is how can I have a single file attachment form field in a custom Wordpress Page Template to follow my current PHP and upload to a custom directory outside of WP?

Thanks in advance for any suggestions!

**Update 12.13.13

I tried using site_url to establish the correct folder path but still no luck. Here's the code I tried so far.

$folderPath = site_url('/jobtracking/files/standard/{$projID}');
mkdir($folderPath);
$filename = basename($_FILES["Attach_Files"]["name"]);
$filetype = $_FILES["Attach_Files"]["type"];
$datei = site_url('/jobtracking/files/standard/{$projID}/{$filename}');
$target_path = $folderPath . "/" . $filename;
    if(move_uploaded_file($_FILES["Attach_Files"]["tmp_name"], $target_path)) {
    mysql_query("INSERT INTO files(files.name, files.project, files.added, files.datei, files.type, files.folder) VALUES('{$filename}', '{$projID}', UNIX_TIMESTAMP(now()), '{$datei}', '{$filetype}', 0)");
    }
chmod("{$target_path}", 0777);

**Update: 01.31.14

It seems that I'm not even getting a directory created on form submission. The folders are all set to 0777 for now just to make sure I have all the read/write privileges I need for my test. I also read about using wp_mkdir_p(); instead of mkdir(); when using Wordpress. I don't know if I can use a variable for the string in the wp_mkdir_p();. As soon as I can get the directory to create and the file to move in, I feel like I'm halfway there.

**Update: 02.03.14

$folderPath = ABSPATH . '/jobtracking/files/standard/{$projID}';

wp_mkdir_p($folderPath, 0777, true);

$filename = basename($_FILES['Attach_Files']['name']);
$filetype = $_FILES['Attach_Files']['type'];
$datei = ABSPATH . 'files/standard/{$projID}/{$filename}';
$target_path = $folderPath . "/" . $filename;
    if(move_uploaded_file($_FILES['Attach_Files']['tmp_name'], $target_path)) {
    mysql_query("INSERT INTO files (files.name, files.project, files.user, files.added, files.datei, files.type, files.folder) VALUES('{$filename}', '{$projID}', 0, UNIX_TIMESTAMP(now()), '{$datei}', '{$filetype}', 0)");
    }
chmod("{$target_path}", 0777);

**Update 02.04.14 - - This creates the directory and puts the file in it. However, information still not entering the database. Maybe I'm not using the if(); correctly?

$folderPath = "/jobtracking/files/standard/{$projID}";

mkdir(ABSPATH.$folderPath, 0777, true);

$filename = basename($_FILES['Attach_Files']['name']);
$filetype = $_FILES['Attach_Files']['type'];
$datei = 'files/standard/{$projID}/{$filename}';
$target_path = ABSPATH.$folderPath . "/" . $filename;
    if(move_uploaded_file($_FILES['Attach_Files']['tmp_name'], $target_path)) {
    mysql_query("INSERT INTO files (files.name, files.project, files.user, files.added, files.datei, files.type, files.folder) VALUES('{$filename}', '{$projID}', 0, UNIX_TIMESTAMP(now()), '{$datei}', '{$filetype}', 0)");
    }
chmod("{$target_path}", 0777);

**Update 02.04.14 - IT WORKS!!! Here's my code:

$folderPath = "/jobtracking/files/standard/{$projID}";

mkdir(ABSPATH.$folderPath, 0777, true);

$filename = basename($_FILES['Attach_Files']['name']);
$filetype = $_FILES['Attach_Files']['type'];
$datei = "files/standard/{$projID}/{$filename}";
$target_path = ABSPATH.$folderPath . "/" . $filename;
    if(move_uploaded_file($_FILES['Attach_Files']['tmp_name'], $target_path)) {
    mysql_query("INSERT INTO files (files.name, files.project, files.user, files.added, files.datei, files.type, files.folder, files.visible) VALUES('{$filename}', '{$projID}', 5, UNIX_TIMESTAMP(now()), '{$datei}', '{$filetype}', 0, ' ')");
    }
chmod("{$target_path}", 0777);
7
  • The directory will never be created. Because you're are using site_url its a URI address, you need directory base path. Try using ABSPATH this is wordpress root path constant. And yes try wp_mkdir_p to create directories. have you tried it? Jan 31, 2014 at 21:26
  • Firstly, thanks @RahilWazir for responding. Any help I can get at this point is monumental. I tried to include your notes in my revision. See my latest code update above which does not work.
    – Matthew
    Feb 3, 2014 at 19:21
  • Which part is not working for you? Have you debugged every single line of your code? Feb 3, 2014 at 19:39
  • It's not creating the directory. That's what I consider my next step. I feel that if I can at least get the PHP to create the directory, I'll be moving in the right direction. Still double-checking the code against everything I can. Using Dreamweaver to write the code and it doesn't spot any syntax errors. I'm still reading about URI and ABSPATH.
    – Matthew
    Feb 3, 2014 at 19:51
  • mkdir(ABSPATH.$folderPath, 0777, true); This created the directory, but now the file does not show up in the directory. One step at a time.
    – Matthew
    Feb 3, 2014 at 20:25

1 Answer 1

0

(Update to answer: 05.27.15) What finally worked is using ABSPATH:

$folderPath = "/jobtracking/files/standard/{$projID}";

mkdir(ABSPATH.$folderPath, 0755, true);

$filename = basename($_FILES['Attach_Files']['name']);
$filetype = $_FILES['Attach_Files']['type'];
$datei = "files/standard/{$projID}/{$filename}";
$target_path = ABSPATH.$folderPath . "/" . $filename;
    if(move_uploaded_file($_FILES['Attach_Files']['tmp_name'], $target_path)) {
    mysql_query("INSERT INTO files (files.name, files.project, files.user, files.added, files.datei, files.type, files.folder, files.visible) VALUES('{$filename}', '{$projID}', 5, UNIX_TIMESTAMP(now()), '{$datei}', '{$filetype}', 0, ' ')");
}
chmod("{$target_path}", 0755);

I also made sure to change the 0777 to 0755.

3
  • 1
    Could you add some details around your code on how to use it and why it answers the question?
    – Tom J Nowell
    Feb 4, 2014 at 20:33
  • 2
    I cringe when I see 0777. If there is a PUT handler on the server, everybody can write anything to that directory. This is really bad.
    – fuxia
    Feb 4, 2014 at 20:48
  • Sorry, didn't see the updates to this question from over a year ago. What worked for me is using the: mkdir(ABSPATH.$folderPath, 0755, true);
    – Matthew
    May 27, 2015 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.