Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);
share|improve this question
    
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? –  Rahil Wazir Jan 31 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 at 19:21
    
Which part is not working for you? Have you debugged every single line of your code? –  Rahil Wazir Feb 3 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 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 at 20:25
show 2 more comments

1 Answer

$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);
share|improve this answer
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 at 20:33
    
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. –  toscho Feb 4 at 20:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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