I am not sure what is wrong here but I know it has to do with move_uploaded_file. I have tried changing the name that the file gets changed to and I have changed the chmod of the directory to 777. From what I can tell there is no reason why the file isn't getting moved, but it isn't.
Here is the code in question. Keep in mind this is part of a Wordpress plugin, so I use functions from there in my code.
if ( isset($_FILES["file"])) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"];
die;
} else {
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
die;
} elseif(strtolower(end(explode('.', $_FILES['file']['name']))) != "csv") {
echo "File is not a .csv";
die;
} else {
if(move_uploaded_file($_FILES["file"]["tmp_name"], plugin_dir_path( __FILE__ ) . "uploads/uploaded_file.csv")) {
echo "Stored in: " . plugin_dir_path( __FILE__ ) . "upload/uploaded_file.csv<br />";
} else {
echo "Temp file was not moved.<br />";
echo '<pre>';
echo 'Here is some more debugging info:<br />';
print_r($_FILES);
print "</pre>";
}
}
}
echo '<form method="post" action="" enctype="multipart/form-data">';
echo '<input type="file" name="file" id="file" />';
submit_button('Import CSV');
echo '</form>';
In the same situation i used: $name = basename($_FILES["file"]["name"]); before calling move_uploaded_file(), i'm not saying it's the solution but maybe you are repeating the path.
Hope it helps.
Related
this is the code i wrote but i keep seeing these 3 dots before the folder name showing up:
<?php
$dir=new DirectoryIterator("wallpapers");
while ($dir->valid())
{
$file=$dir->current();
echo $file->getFilename();
echo "<br>";
$dir->next();
}
?>
You should be good to go with this
$dir = "/etc/php5/*";
foreach(glob($dir) as $file)
{
echo "filename: $file : filetype: " . filetype($file) . "<br />";
}
So I have 4 pages. They are very simple.
index.php (WORKS)
<html>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br />
<input type="submit" value="Now upload it!">
</form>
</html>
upload.php (WORKS)
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
echo 'Are you sure you want to continue saving this file';
echo 'Yes, continue
<br />
<br />
No thanks'
}
?>
no.php (WORKS)
<?php
echo 'Thanks anyway';
?>
yes.php (ERROR)
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
echo 'We will now save this document:';
//Save document code
}
?>
Output of yes:
Notice: Undefined index: file in /home/public_html/test/yes.php on line 2 Invalid file
We will now save this document:?
As you can see I never save it. But I would like to save it in the yes.php page. Is it still possible to retrieve that original doc that was uploaded? Thanks in advance.
Uploaded files are only available for a single PHP instance/request cycle.
Uploaded files are stored in the temp directory. If they're still there when the script finishes executing, PHP will delete them assuming you didn't need them.
If you want to persist the file, you'll have to move it elsewhere in the same request that the file was uploaded.
You are trying to pass files from a form to a different page then intended. The post values will no longer be valid. I would suggest saving the file in the upload.php to a temporary folder and from there passing it to either the yes or no page via a $_GET[] or session variable.
On the no.php page you would take that file and use
unlink($somefile);
This will delete the file from your server.
On the yes.php page I would move or copy the file. If you copy the file, I would use unlink to remove the temp file.
You could try using move_uploaded_file() to temporarily save the file and then pass the file information using
Yes, continue
No thanks
Then retrieve the filename in yes.php using
$tmpFile = $_GET["filename"];
or remove it in no.php using
unlink($_GET["filename"]);
If the file is not too large, copy the file content in a session variable. The session will be preserved from one page to another.
I have a script in php that is used to upload files to a server. It was working first but i dont know why its not working again. It shows no error but the file is not still uploaded to the directory that i assigned to hold all uploaded files. Here is the part that takes care of the upload:
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Uploaded: " . basename($_FILES["file"]["name"]) . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
//by default the size of the file is in bytes so u need to divide by 1024 in order to bring it to KB
echo "Temporarily stored in: " . $_FILES["file"]["tmp_name"] . "<br />";
$target="/file/" . basename($_FILES["file"]["name"]) . "<br />";
}
if(move_uploaded_file($_FILES["file"]["tmp_name"], $target))
{
echo "<h2>" . "The file has been stored on the server" . "<br />" . "<h2 />";
echo "New storage location is : " . '<a href="/public/files/" >' . $target . '</a>';
?>
<html>
<body>
<div align="right"><a href="/public/files/" >Uploaded files</a></div>
<br />
</body>
</html>
<?php
}
else
{
echo "<h2>" . "Error while saving the file to the server." . "<br />" . "File wont be found in the uploaded files directory" . "<br />" . "<h2 />";
echo "The error says: " . $_FILE["file"]["error"] . " What do we do now?" ;
echo"<pre>".print_r($_FILES,true)."</pre>";
?>
<?
/file/ is a directory in the root of your server's filesystem, which almost certainly doesn't exist. move_uploaded_file() works at the filesystem level and has absolutely NO awareness of your site's URI structure. You probably want something more like:
move_uploaded_file(...,. $_SERVER['DOCUMENT_ROOT'] . '/file/');
^^^^^^^^^^^^^^^^^^^^^^^^^^^---- add this
so that the file gets moved to a /file subdir of your site's root directory.
I have included an upload script, which currently ONLY checks the upload folder to make sure that the file isn't there, which works just fine. However, I would like to somehow silently include a search function to see if it is already in one of the other directories.
The process: User uploads a prl, it checks upload folder if its there. This works. I need it to check another directory (recursively) to see if it is in any subdirectory already, and then list them out.
Just a note: $carrier is an existing subdirectory. I would like it to check the parent directory and all of its subs for the filename minus the extension, and if it exists, Error: this file already exists for "this" carrier.
<?php
$allowedExts = array("prl");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$carrier = $_POST['carrier'];
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
if (file_exists("./prls/" . $carrier . "/" . $_FILES["file"]["name"])){
echo "<h2><u>Error:</u></h2>";
echo $_FILES["file"]["name"] . " is already listed in the " . $carrier . " directory. ";
echo "<hr><br />If you feel this PRL is listed incorrectly, please let us know.";
die;
}
if (file_exists("upload/" . $_FILES["file"]["name"] . "-" .$carrier)) {
echo "<h2><u>Error:</u></h2>";
echo $_FILES["file"]["name"] . " for <b>" . $carrier . "</b> has already beeb submitted for approval. ";
die;
}
if ($_FILES["file"]["error"] == 0 && in_array($extension, $allowedExts))
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"] . "-" . $carrier);
echo "<h2><u>Submitted for approval:</u></h2>";
echo $_FILES["file"]["name"] . " for " . $carrier . ". <b>Thank you</b>.";
}
else
{
echo "Invalid file. Please choose a PRL with a \".prl\" extension.";
}
?>
You want to use RecursiveIteratorIterator. Here's a function that will behave like file_exists():
function file_exists_recursive($dir, $filename) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST );
foreach($iterator as $path) {
if (!$path->isDir()) {
if(file_exists($filename.$path->__toString()))
return true;
}
return false;
}
}
Then call this function in your file:
if (file_exists_recursive("upload/", $_FILES["file"]["name"] . "-" .$carrier)) {
echo "<h2><u>Error:</u></h2>";
echo $_FILES["file"]["name"] . " for <b>" . $carrier . "</b> has already beeb submitted for approval. ";
die;
}
So I have 4 pages. They are very simple.
index.php (WORKS)
<html>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br />
<input type="submit" value="Now upload it!">
</form>
</html>
upload.php (WORKS)
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
echo 'Are you sure you want to continue saving this file';
echo 'Yes, continue
<br />
<br />
No thanks'
}
?>
no.php (WORKS)
<?php
echo 'Thanks anyway';
?>
yes.php (ERROR)
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
echo 'We will now save this document:';
//Save document code
}
?>
Output of yes:
Notice: Undefined index: file in /home/public_html/test/yes.php on line 2 Invalid file
We will now save this document:?
As you can see I never save it. But I would like to save it in the yes.php page. Is it still possible to retrieve that original doc that was uploaded? Thanks in advance.
Uploaded files are only available for a single PHP instance/request cycle.
Uploaded files are stored in the temp directory. If they're still there when the script finishes executing, PHP will delete them assuming you didn't need them.
If you want to persist the file, you'll have to move it elsewhere in the same request that the file was uploaded.
You are trying to pass files from a form to a different page then intended. The post values will no longer be valid. I would suggest saving the file in the upload.php to a temporary folder and from there passing it to either the yes or no page via a $_GET[] or session variable.
On the no.php page you would take that file and use
unlink($somefile);
This will delete the file from your server.
On the yes.php page I would move or copy the file. If you copy the file, I would use unlink to remove the temp file.
You could try using move_uploaded_file() to temporarily save the file and then pass the file information using
Yes, continue
No thanks
Then retrieve the filename in yes.php using
$tmpFile = $_GET["filename"];
or remove it in no.php using
unlink($_GET["filename"]);
If the file is not too large, copy the file content in a session variable. The session will be preserved from one page to another.