I'm using this image uploader:
http://www.jotform.org/jquery/ajax-multi-file-upload-with-progress-bar/#more-291
It's jQuery/Ajax, while I have it working I know little about either language. I want to modify it so that the filenames of all uploaded files get put into a database.
The included upload.php file doesn't appear to work as I've deleted that and the uploader is still functioning. If that was required then I could quite easily write the required PHP to do the job.
One solution would be for a PHP file to be executed after the upload finishes, how can I do that?
Appreciate any help here.
<form id="upload" method="post" action="upload.php" enctype="multipart/form-data">
<div id="drop">
Drop Here
<a>Browse</a>
<input type="file" name="upl" multiple />
</div>
<ul>
<!-- The file uploads will be shown here -->
</ul>
</form>
Edited above - this is the form that the upload uses, is there anyway I could use an "isset" to check it's been used, there's no submit button though.
The above code is what I've got on the page with the uploader on it, it doesn't seem to actually call the upload.php file.
I've not used this file uploader before. Having looked at it though, upload.php is responsible for processing the file after upload.
If you look at this script:
if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){
echo '{"status":"success"}';
exit;
}
You can see that on successful upload, it moves the file to the included uploads directory. It's at this point that you could extend this script and save $_FILES['upl']['name'] to a database. You might want to look in to the possible PHP database extensions
$('#upload').fileupload({
// This element will accept file drag/drop uploading
dropZone: $('#drop'),
// This function is called when a file is added to the queue;
{***SNIP***}
});
// Automatically upload the file once it is added to the queue
var jqXHR = data.submit();
},
The second to last line var jqXHR = data.submit(); is when it submits the form. That's the AJAX part of it. Since you deleted the upload.php file, it actually gives a 404. However, the code was poorly written and doesn't seem to alert you it cant reach said file.
Nonetheless, upload.php IS getting called, and IS being used. As someone else above suggested, you can extend that to get any details about the uploaded files you need. (And actually, I would put any logic before the echo. That echo is what tells the JavaScript everything is hunky-dorey.)
Related
The purpose is to allow user uploading files (usual uploading process) and confirm uploading by pressing on the confirmation button. Programming side: 2 folders - 1 for unconfirmed files where files get deleted periodically and 2 - confirmed folder - where files are copied from unconfirmed folder if a user presses the confirmation button.
Basically, I have a form, where a user uploads files that are stored in the folder and the path to it - in a database. And on the same page I have another button, so when the user presses it I would like to move his/her uploaded files from one directory to another (not re-uploading - coping on server).The issue is taht I should know WHICH files to deal with!
My problem is: everything seems ok, but I can't get the variable $name - that tells me the name of the file, user has uploaded. Because When I try to use it to proceed second submit button - it says variable is undefined. I need to get the variable that tells me the name of the file user has submitted so I can copy that file to another directory, but it can only be assigned (and unfornunatelly used) when procesing the form submittinf and enabled to use only inside of the processing, while I want to use it outside.
I have tried to use sessions - but this code doesnt work, same problem - it says that - 'undefined index - regex', same with COOKIES.
All the html:
<form action="videator.php"method="post" enctype="multipart/form-data">
<input class="form" type="file" name="fileToUpload" id="fileToUpload" accept="video/*" >
<input class="form" type="submit" value="Upload Image" name="submit">
</form>
<form action="love.php" method="post">
<input class="post" type="submit" value="POST" name="post" id="post"/>
</form>
videator.php:
if(isset($_POST["submit"])) {
$file = $_FILES['fileToUpload'];
$name = $file['name'];
$_COOKIE['name'] = $name;
$_SESSION['regex'] = $name;
...
}
love.php
if(isset($_POST["post"])) {
session_start();
$name = $_GET['regex'];
$from = "temp_videos/".$name;
$to = "videos/";
if (!copy($from, $to)) {
echo("<script>alert('fail')</script>");
}else {
echo("<script>alert('Success!')</script>");
}
It says also that 'The first argument to copy() function can not be a directory. I guess, that's due to the fact that my variable $name is undefined, therefore only the folder is here as the path.
Please, help. I've spent all my day on that.
(If I just use copy() function with indicating actual path and not a variable of it - everything works)
Thanx in advance.
I have a few tips that may help.
(1) At the top of each PHP file, as the first instruction, put:
<?php
if (!session_id()) session_start();
//any other PHP instructions follow.
?>
(2) Try using the excellent jQuery File Upload plugin by Ravi Kusuma. Although I generally try to code everything myself and not use plugins, this one is so well done that it's my primary goto for file uploads.
(3) Until you get this working, there is no need to put the session_start() inside an if statement. The session_start() must be the first instruction at top of each PHP page that references the $_SESSION variable.
(4) Look into AJAX instead of using <form> (for managing the file upload process). Kusuma's plugin also works great with AJAX. If you haven't used AJAX before, it may sound intimidating - but it's super simple. Copy the examples at the bottom of this post and make them work on your system. It may be the most important 15 mins you've spent in 2016. Note that the point of the linked question is that there must be a separate PHP file that receives the AJAX post.
(5) Don't forget that AJAX is a two-step process: (a) the javascript on the current page communicates with the specified PHP page; (b) the PHP page receives the data (or file upload data) via POST varibles. Therefore, be sure to check out the "SERVER SIDE" tab at top of Kusuma's Hayageek page. At the top of the SERVER SIDE page are four links to sample PHP files. Study the upload.php example.
Happy coding.
I am trying to upload a file a view the file on an iFrame but it is not working.
Here is what I have tried
jQuery('.file_upload1').change(function(){ jQuery('iframe').attr('src', jQuery(this).val()); $('iframe').attr('src', $('iframe').attr('src')); });
<label><input class="file_upload1" name="file_cover" type="file"></label>
<div>
<iframe src=""></iframe>
</div>
If this does not work, can I move the uploaded file to server directory so that the path becomes valid? How would I do this?
Apart from other problems and limitations of such solution, and specifically answering "why it does not work?" question:
The change event needs to be nested in ready function:
jQuery("document").ready(function() {
jQuery('.file_upload1').change(function() {
jQuery('iframe').attr('src', jQuery(this).val());
});
});
The src attribute of iframe must be a valid non-empty URL potentially surrounded by spaces. When selecting a file with input type file, in Windows the value will be something like c:\fakepath\file name goes.here, so before using as iframe source, you will have to rework it a little bit.
Regarding
I'm trying upload file and display it on iframe forcing it to reload
You don't need to force reload to achieve this. "Upload" means the page will be reloaded (well, unless upload is handled using AJAX, but it's not the case here I guess). In order to upload a file, the form must be submitted and the page needs to be reloaded. Once reloaded, you can easily construct the full path of the file and use it in iframe, something like:
<iframe src="<?php echo $file_full_url; ?>"></iframe>
But if you want to preview the file in iframe before uploading to server - it won't be possible due to security reasons. See this question for reference: How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?
Ok, I've got a form and i've parsed all the data and i'm ready to write it to the file. I use the following PHP code (simplified):
$file = fopen("Data.txt","a");
fwrite($file,$_GET["InputText"]);
fclose($file);
It writes just fine to the file but opens a new blank page. How do i stop it from doing that?
Thanks for the help
-Dave
EDIT: No output generated by PHP. It just writes a line of text to a file. I am using a seperate file to hold my PHP code...
< form id="fmInput" action="IM.php" onsubmit="submitText()" >
How else can i do that?
EDIT2: Heres more of my code:
<form id="fmInput" action="IM.php" onsubmit="submitText()">
<input type="text" name="fnInputText" id="iInputText">
</form>
<?php
//IM.php
$file = #fopen("IMData.txt","ab");
fwrite($file,$_GET["fnInputText"]);
fwrite($file,"<br>");
fclose($file);
?>
Now, when the user hits enter on the form, JS captures and processes (using the submitText function) then PHP writes it to a file, but then opens a blank browser...
EDIT3: I'm guessing its a blank page being loaded in the same window because it's got this in the address bar "/IM.php?fnInputText="
I dont want it to do that. I need it to write to the file without any interuption, so having PHP display anything after it runs is a deal breaker. Even if i have to have it reload the page that'd be better... It'll be marginally more time consuming as JS will have to reload the file again...
I figured out how to read from a file using JS and the XMLHttpRequest but for the life of me i cant get JS to write to the file. I'd perfer to use JS as I know very little about PHP (which is probably aparent from this post ;D)
My goal is this: page loads, JS reads in file and displays file on screen. User types in somthing, hits enter, JS adds that to screen and then [PHP or JS] writes data to file. repeat.
Perhaps you want to redirect your user to a non-blank page after you're done writing your data?
header("Location: /back-to-my-page.html");
You must be submitting the form data to another file. For example, doing an
onsubmit="write.php"
That should open a new page and write to the file
Here is the whole package!
IM.php:
<?php
// write to file if the form is submitted
if(isset($_POST['submitted'])){ # if form is submitted
$secure_input = htmlentities($_POST['input_text']); # making sure no malicious html code is submitted
file_put_contents('some_file_to_write.txt', $secure_input); #write secure_input to a file
echo '<br>completed!</br>';
}
?>
<form id="fmInput" action="IM.php" method="post">
<input type="text" name="fnInputText" id="input_text">
<input type="submit" name="submitted" value="write to file" />
</form>
if any errors comes up : you do not have permission to write! and if the file gets downloaded you do not have php installed ;)
Good Luck!
I have a form which takes a couple of text field inputs and then a folder path.
Now before I submit the form I want to make sure that whatever the folder path the user specified is correct, if not print an error message.
Is there a way I can validate this in the same page before I submit the form?
I used javascript, but it doesnt seem to work as I expected. Thoughts/Suggestions ?
<script>
function checkfolder()
{
var myObject;
myObject = new ActiveXObject("Scripting.FileSystemObject");
if(!myObject.FolderExists()){
alert("Folder does not exist");
}
}
</script>
<form method=post action="some_file.php">
.
.
.
<input type="submit" name="submit" value="submit" onClick='checkFolder()'>
</form>
You're not going to have much luck with this. PHP can't do this because it operates on the server and has no access to the user's computer. JavaScript will fail because browser prevent access to the file system with JavaScript for security reasons.
You don't have any way of accessing the local data to validate its existence. Even if you do, it's considered really bad.
Instead of writing the file path (which I assume that's what you are doing), just make a javascript Browse file dialog like when you upload an image or file to Gmail. That kind. This ensures that it exists at least when you are trying to upload the file.
Whether the file actually gets deleted after you have selected the file, and before you submit it. It doesn't matter. If it doesn't exist, the upload will fail.
<form action="upload_file.php" method="post" enctype="multipart/form-data">
Above is the line for a form to upload a file using php.
If i use the form as I wrote above I can get the various properties of the file to be uploaded from upload_file.php page.
Using jquery $.post , how can I do the same thing in upload_file.php ?
I googled but can't find the exact snippet of code necessary here.
Uploading images using jquery's ajax methods is a pain, generally involving dynamically creating an iframe and submitting a form inside it. I usually use this plugin: http://plugins.jquery.com/project/jquery-upload
You can write some php to echo the uploaded image data as a json object, which you can access once the upload is complete through the plugin.