I have a php file named "savegame.php".
I use xampp. When i enter "localhost/morabaraba/savegame.php" as a url manually it works
i have a file "index.html" in the same folder as my "savegame.php" file and using window.location = "savegame.php" redirects
to c:\....\morabaraba\savegame.php but it does not echo back or update the database as the code in "savegame.php" says it should
code in savegame.php
<?php
print 'i am working';
//database updating details
?>
When i say enter localhost/morabaraba/savegame.php i mean typing it in the window, not coding it as window.location = "local/morabaraba/savegame.php" in the index.html file
Related
Im trying to make a website with users on it, and I'm trying to create a script so that whenever a new user registers it will automatically create a user folder and profile for them but when I try to register it doesn't create the files, could someone please help me with this, Thanks
<?php
include "inc/header.php";
$newfolder = $username;
if (!mkdir($newfolder, 0777, true)) {
die('Failed to create folders...');
}
$pagename = $username;
$newFileName = './u/'.$username.'/'.$pagename.".php";
$newFileContent = '<?php echo "something..."; ?>';
?>
To make a directory/file
if (!file_exists("parent_folder/$username")) {
//Create a file with read write execute PERMISSIONS ENABLED
//Please check : your parent folder also must have 0777 permissions to avoid any kind of read write error
mkdir("parent_folder/$username", 0777);
//now u have to create a FILE with .php
//now this file_put_contents is VERY importnant !
$pagename = $username ;
$newFileName = './parent_folder/$username/'.$pagename.".php";
$newFileContent = '<?php echo "something..."; ?>';
if (file_put_contents($newFileName, $newFileContent) !== false) {
//notify file is created
echo "File created (" . basename($newFileName) . ")";
} else {
//notify u have error
echo "Cannot create file (" . basename($newFileName) . ")";
}
//now create ur .php file in user folder
}
else {
echo "Your parent folder does not exist"
}
Now the possible error and some tips
1) Most of people do fopen("filename_with_PATH", "w")
and expect that file will be generated in PATH folder !
Some times it might fall wrong (depends on version)
fopen is meant to create a file in the directory where your php resides
2) check ur php permission in php.ini if u dont give php permission to write,remote access then u might get some errors (it will be displayed that u have error in my script)
3)For more info and tinkering file_put_contents
Hope this will be helpful for you ..
hello friend i am making an aplication based on web plataform html5 css3 ...
and i have a web site with this, but i thought creat a direct link to shout(pop) this aplication to end user from the desktop.
example:
Descarga Aplicativo
but it not work on chrome or other browser. i get error file or uncomplete download. file named: IcarosNetWeb.downkload ....
Can someone help me with this?
Solved by this way
Direct Access
As not able to find the way to do it directly. I created a free mechanism:
1. Create a .bat file (Batch command line) Hiden.(This is the task: create the shortcut to the application with its parameters.)
Compressing the .bat file within a file self-extracting WinRAR and run the .bat file at the end of the extraction, automatically.
Create Hidden Log File on C:/ if have problem executing.
AutoInstall.bat file
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET LinkName=MyAplication
SET Esc_LinkDest=%%HOMEDRIVE%%%%HOMEPATH%%\Desktop\!LinkName!.lnk
SET Esc_LinkTarget=%%ProgramFiles(x86)%%\Google\Chrome\Application\chrome.exe
SET Esc_WorkDir=%%ProgramFiles(x86)%%\Google\Chrome\Application
SET Esc_Icon=%%ProgramFiles(x86)%%\Google\Chrome\Application\chrome.exe,6
SET Esc_URL=www.google.com
SET Esc_ARG=--app=http://!Esc_URL!/
SET cSctVBS=CreateShortcut.vbs
SET LOG=".\%~N0_runtime.log"
((
echo Set oWS = WScript.CreateObject^("WScript.Shell"^)
echo sLinkFile = oWS.ExpandEnvironmentStrings^("!Esc_LinkDest!"^)
echo Set oLink = oWS.CreateShortcut^(sLinkFile^)
echo oLink.TargetPath = oWS.ExpandEnvironmentStrings^("!Esc_LinkTarget!"^)
echo oLink.WindowStyle = 1
echo oLink.IconLocation = oWS.ExpandEnvironmentStrings^("!Esc_Icon!"^)
echo oLink.Arguments = !Esc_URL!
echo oLink.WorkingDirectory = oWS.ExpandEnvironmentStrings^("!Esc_WorkDir!"^)
echo oLink.Description = "Internet Access"
echo oLink.Save
)1>!cSctVBS!
cscript //nologo .\!cSctVBS!
DEL !cSctVBS! /f /q
)1>> !LOG! 2>>&1
attrib +h !LOG!
in Winrar:
put SFX File option, go Advance Tab and Click SFX FIle, on tab General add folder Extraction C:/ and on Instaltion tab in Instaltion after Extraction field add the full name (no path) .bat aplication.
Hello ive searched everywhere to find the answer however none of the solutions ive tried helped
What i am building is a site which connects to Youtube to allow users to search and download videos as MP3 files. I have built the site with the search etc however i am having a problem with the download part (ive worked out how to get the youtube audio file). The format for the audio is originally audio/mp4 so i need to convert it to mp3 however first i need to get the file on the server
So on the download page ive made a script that sends an ajax request to the server to start downloading the file. It then sends a request to a different page every few seconds to find out the progress and update it on the page the user is viewing.
However the problem is while the video is downloading the whole website freezes (all the pages dont load until the file is fully downloaded) and so when the script tries to find out the progress it cant until its fully done.
The file which downloads:
<?php
session_start();
if (isset($_GET['yt_vid']) && isset($_GET['yrt'])) {
set_time_limit(0); // to prevent the script from stopping execution
include "assets/functions.php";
define('CHUNK', (1024 * 8 * 1024));
if ($_GET['yrt'] == "gphj") {
$vid = $_GET['yt_vid'];
$mdvid = md5($vid);
if (!file_exists("assets/videos/" . $mdvid . ".mp4")) { // check if the file already exists, if not proceed to downloading it
$url = urlScraper($vid); // urlScraper function is a function to get the audio file, it sends a simple curl request and takes less than a second to complete
if (!isset($_SESSION[$mdvid])) {
$_SESSION[$mdvid] = array(time(), 0, retrieve_remote_file_size($url));
}
$file = fopen($url, "rb");
$localfile_name = "assets/videos/" . $mdvid . ".mp4"; // The file is stored on the server so it doesnt have to be downloaded every time
$localfile = fopen($localfile_name, "w");
$time = time();
while (!feof($file)) {
$_SESSION[$mdvid][1] = (int)$_SESSION[$mdvid][1] + 1;
file_put_contents($localfile_name, fread($file, CHUNK), FILE_APPEND);
}
echo "Execution time: " . (time() - $time);
fclose($file);
fclose($localfile);
$result = curl_result($url, "body");
} else {
echo "Failed.";
}
}
}
?>
I also had that problem in the past, the reason that it does not work is because the session can only be once open for writing.
What you need to do is modify your download script and use session_write_close() each time directly after writing to the session.
like:
session_start();
if (!isset($_SESSION[$mdvid])) {
$_SESSION[$mdvid] = array(time(), 0, retrieve_remote_file_size($url));
}
session_write_close();
and also in the while
while (!feof($file)) {
session_start();
$_SESSION[$mdvid][1] = (int)$_SESSION[$mdvid][1] + 1;
session_write_close();
file_put_contents($localfile_name, fread($file, CHUNK), FILE_APPEND);
}
I am trying to create and save a file to the root directory of my site, but I don't know where its creating the file as I cannot see any. And, I need the file to be overwritten every time, if possible.
Here is my code:
$content = "some text here";
$fp = fopen("myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
How can I set it to save on the root?
It's creating the file in the same directory as your script. Try this instead.
$content = "some text here";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
If you are running PHP on Apache then you can use the enviroment variable called DOCUMENT_ROOT. This means that the path is dynamic, and can be moved between servers without messing about with the code.
<?php
$fileLocation = getenv("DOCUMENT_ROOT") . "/myfile.txt";
$file = fopen($fileLocation,"w");
$content = "Your text here";
fwrite($file,$content);
fclose($file);
?>
This question has been asked years ago but here is a modern approach using PHP5 or newer versions.
$filename = 'myfile.txt'
if(!file_put_contents($filename, 'Some text here')){
// overwriting the file failed (permission problem maybe), debug or log here
}
If the file doesn't exist in that directory it will be created, otherwise it will be overwritten unless FILE_APPEND flag is set.
file_put_contents is a built in function that has been available since PHP5.
Documentation for file_put_contents
fopen() will open a resource in the same directory as the file executing the command. In other words, if you're just running the file ~/test.php, your script will create ~/myText.txt.
This can get a little confusing if you're using any URL rewriting (such as in an MVC framework) as it will likely create the new file in whatever the directory contains the root index.php file.
Also, you must have correct permissions set and may want to test before writing to the file. The following would help you debug:
$fp = fopen("myText.txt","wb");
if( $fp == false ){
//do debugging or logging here
}else{
fwrite($fp,$content);
fclose($fp);
}
I have a page with some forms and input fields, which the user fills in and then they are sent to a php page via Ajax and $_POST.
And then the php File writes the output to a txt file. - that works just fine. My problem is I am trying to force the user to download it on that same page that creates the file, after the file is created and I can't seem to get it to work, nothing happens besides the file being created:
Here the code where the .txt File is created (this works nice):
$myfile = fopen("test.txt", "w") or die("Unable to open file!");
foreach ($URLsArray as &$url) {
$row=$SomeArray[$keys[$index]]."\t".$SomeArray[$keys[$index]]."\t".$SomeArray[$keys[$index]]."\t".$SomeArray[$keys[$index]]."\t".$url."\n";
fwrite($myfile, $zeile);
$index = $index + 1;
}
fclose($myfile);
And here the code, where I try to force the download: (after the fclose)
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('test.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('test.txt'));
readfile('test.txt');
exit;
And when I try this I get the error: "Unexpected token A". "A" is the first letter in the test.txt, which is created.
And I know that there are a lot of similar questions, but not one solution worked for me.
I hope someone can help me :)
Instead of doing this backend with PHP you could try to do this on the frontend part. The easiest solution is to write some JavaScript code which adds an iframe to your webpage. The iframe should then have a href to the file you want the user to download.
Okay here my solution: #CBroe thanks for the hint with the background request, I would have tried for ages to get the download work in the php file. So what I did:
In PHP: I echo the filename after the .txt file is created:
echo json_encode(array('filename' => "your_Data".time().".txt"));
Than in JavaScript I read the filename in the success method and put the link in an <a> element. Later that will be a button which is set active.
success : function(data) {
$("#button_get_file_download").attr("href", "urlToFolder"+data.filename);
},