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);
site_url
its a URI address, you need directory base path. Try usingABSPATH
this is wordpress root path constant. And yes trywp_mkdir_p
to create directories. have you tried it?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.