Currently my project is built in cakephp 2. I am moving stuffs to node.js from cakephp. I have a piece of code in core.php file. My core.php file includes these piece of code:
$bucket = env('PRJECTNAME_S3_BUCKET');
$imgPath = env('BASE_PATH_IMG');
Configure::write('Files.S3Bucket', $bucket);
if (!empty($imgPath)) {
Configure::write('Files.url', $imgPath);
} else if (empty($bucket)) {
if (!empty($httpHost)) {
$s = null;
if (env('HTTPS')) {
$s = 's';
}
$httpHost = 'http' . $s . '://' . $httpHost;
}
Configure::write('Files.url', $httpHost);
} else {
Configure::write('Files.url', 'https://s3.eu-central-2.amazonaws.com/' . $bucket);
}
$filespath = Configure::read('Files.url');
if (substr($filespath, 0, strlen('http://localhost')) == 'http://localhost') {
Configure::write('Files.safeurl', '');
} else {
Configure::write('Files.safeurl', $filespath);
}
Whenever I need to use the path for image I used to invoke it Configure::read('Files.url') in this way.
As a said I am moving our code to node.js from cakephp. I need to write these piece of code in node.js.
Can anyone help me out to converting this cakephp code to node.js?
Related
I am building a Laravel app in which I upload videos.
To recover the duration of each video, I use the following package which is very good: https://github.com/JamesHeinrich/getID3/
Where I need help is to get the total duration of all the videos and I have no idea how to handle this.
Here is an image that illustrates my research:
And this is how I call GetID3 in my code:
<?php
namespace App\Http\Managers;
use Illuminate\Support\Facades\Auth;
class VideoManager
{
public function getVideoDuration($Videofile)
{
$getID3 = new \getID3();
$pathVideo = 'storage/chapitres-videos/' . Auth::user()->id . '/' . $Videofile;
$fileAnalyze = $getID3->analyze($pathVideo);
$playtime = $fileAnalyze['playtime_string'];
return $playtime;
}
}
$Videofile = $this->videoManager->videoStorage($request->file('video'));
$lecture->video = $Videofile;
I am trying to make a ajax call and validate a input html field. But, instead of getting simple echo message. I am getting complete source code in responseText.
JavaScript
function checkUsername() {
document.getElementById("username").className = "thinking";
usernameRequest = createRequest();
if (usernameRequest == null)
alert("Unable to create request");
else {
var theName = document.getElementById("username").value;
var username = escape(theName);
var url= "checkName.php?username=" + username;
usernameRequest.onreadystatechange = showUsernameStatus;
usernameRequest.open("GET", url, true);
usernameRequest.send(null);
}
}
function showUsernameStatus() {
alert(usernameRequest.responseText);
if (usernameRequest.readyState == 4)
{
if (usernameRequest.status == 200) {
if (usernameRequest.responseText == "okay") {
document.getElementById("username").className = "approved";
document.getElementById("register").disabled = false;
}
else {
document.getElementById("username").className = "denied";
document.getElementById("username").focus();
document.getElementById("username").select();
document.getElementById("register").disabled = true;
}
}
}
}
checkName.php
<?php
$takenUsernames = array('bill', 'ted');
sleep(2);
if (!in_array($_REQUEST['username'],$takenUsernames )) {
echo 'okay';
} else {
echo 'denied';
?>
Previously, I tried to integrate PHP into tomcat, but I was advice it was not a good practice. TRIAL TO INTEGRATE PHP
What I can make out of this situation is that Tomcat is not parsing PHP file and instead it is returning the source code. I believe there should be a means for me to let tomcat parse php files and send the right response.
I have also tried with simple php code, with one statment <?php echo 'HELLO'; ?> and I still get the source code.
Thanks in advance.
NOTE : I do not know php, I am working an example from HEAD FIRST AJAX
you need to install PHP for Tomcat & set its path to compile it.see the below link for php configuration settings.
http://php-java-bridge.sourceforge.net/doc/tomcat6.php
http://www.studytrails.com/blog/php-on-a-java-app-server-apache-tomcat-using-quercus/
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm starting with web programming with javascript and i have a little problem with uploading file.
I find this: http://tutorialzine.com/2013/05/mini-ajax-file-upload-form/
but I have no idea how to use upload.php file in mvc project. What to use as an action of form and where should be plasted this php code?
Can anyone give mi some hints?
you can upload you're file with ajax if you create a controller with php:
<?php
$valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // Autorized extensions
$max_size = 200 * 1024; // Max file size
$path = 'uploads/'; // Folder where to upload
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
if( ! empty($_FILES['image']) )
{
// Get the extension of the file
$ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
// Test the file format if it's allowed
if (in_array($ext, $valid_exts) AND $_FILES['image']['size'] < $max_size)
{
$path = $path . uniqid(). '.' .$ext;
// Put the file in the folder of uploads
if (move_uploaded_file($_FILES['image']['tmp_name'], $path))
echo $path; // returning the path
else
echo 'uploads/err.gif'; // returning the error message or path or whatever
}
else
echo 'uploads/err.gif';
}
else
echo 'uploads/err.gif';
}
else
echo 'uploads/err.gif';
?>
In you're view, you have to include jQuery.form library and. you create a form, and inside the form you put an image and a button, the form should have the link of you're php file as action, and call this function:
$('#form').ajaxUpload($('#button'),$('#image_preview'));
The definition of ajaxUpload function is this:
jQuery.fn.ajaxUpload = function(Button,Preview)
{
var Frm = $(this); // form
var Btn = Button; // upload button
var Prev = Preview; // preview area
Btn.click(function()
{
// implement with ajaxForm Plugin
Frm.ajaxForm(
{
beforeSend: function()
{
Btn.attr('disabled', 'disabled');
Prev.fadeOut();
},
success: function(Result)
{
Frm.resetForm();
Btn.removeAttr('disabled');
Prev.attr("src",Result).fadeIn();
},
error: function(Result)
{
Btn.removeAttr('disabled');
Prev.attr("src",Result).fadeIn();
}
});
});
};
var Request = require("request").Request;
var latestTweetRequest = Request({
url: "http://api.twitter.com/1/statuses/public_timeline.json",
onComplete: function (response) {
var tweet = response.json[0];
console.log("User: " + tweet.user.screen_name);
console.log("Tweet: " + tweet.text);
}
});
// Be a good consumer and check for rate limiting before doing more.
Request({
url: "http://api.twitter.com/1/account/rate_limit_status.json",
onComplete: function (response) {
if (response.json.remaining_hits) {
latestTweetRequest.get();
} else {
console.log("You have been rate limited!");
}
}
}).get();
The above is the sample code given in this page . https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/request.html . What I know is :
Simple case :
We have main.js and script.js . We usually write API(SDK) related things in main.js and our normal javascript in script.js . I have tried copying the above code to main.js but didnt work .
Requirement : Simple example containing main.js and script.js showing the usage of ajax call with a local file . For example, please upload samplefile.txt with some text saved in it to the 'data' directory and do some ajax call and get the data from the file. Thats it .
I am trying to write a webpage for a list of files to download. The files are stored with the webpage and I want the webpage to dynamically list all the files in the folder to download. That way when more are added I don't have to modify the webpage. I know how to use JavaScript to create links on the webpage but I need to use it to find the names of the files first.
I found a website that had code for navigating files like a file browser but it only uses a string to store the current location.
This is in the header:
<script type="text/javascript"><!--
var myloc = window.location.href;
var locarray = myloc.split("/");
delete locarray[(locarray.length-1)];
var fileref = locarray.join("/");
//--></script>
this is in the body:
<form>
<input type=button value="Show Files" onClick="window.location=fileref;">
</form>
However this doesn't really help since I am trying to create download links to files not have a file browser.
Edit:
When you host a traditional HTML page you upload the htmlfile and any images or content for the page to what ever server you use.
I want to use javascript to dynamically link to every file hosted with the webpage.
I am trying to combine this with hosting the files in a Dropbox public folder for a simple way to make the files available.
If you want a list of files on the server you will need to use a server-side script to gather their names:
JS--
//use AJAX to get the list of files from a server-side script
$.getJSON('path/to/server-side.php', { 'get_list' : 'true' }, function (serverResponse) {
//check the response to make sure it's a success
if (serverResponse.status == 'success') {
var len = serverResponse.output.length,
out = [];
//iterate through the serverResponse variable
for (var i = 0; i < len; i++) {
//add output to the `out` variable
out.push('<li>' + serverResponse.output[i] + '</li>');
}
//place new serverResponse output into DOM
$('#my-link-container').html('<ul>' + out.join('') + '</ul>');
} else {
alert('An Error Occured');
}
});
PHP--
<?php
//check to make sure the `get_list` GET variable exists
if (isset($_GET['get_list'])) {
//open the directory you want to use for your downloads
$handle = opendir('path/to/directory');
$output = array();
//iterate through the files in this directory
while ($file = readdir($handle)) {
//only add the file to the output if it is not in a black-list
if (!in_array($file, array('.', '..', 'error_log'))) {
$output[] = $file;
}
}
if (!empty($output)) {
//if there are files found then output them as JSON
echo json_encode(array('status' => 'success', 'output' => $output));
} else {
//if no files are found then output an error msg in JSON
echo json_encode(array('status' => 'error', 'output' => array()));
}
} else {
//if no `get_list` GET variable is found then output an error in JSON
echo json_encode(array('status' => 'error', 'output' => array()));
}
?>