filoaman Posted May 27, 2016 Share Posted May 27, 2016 Hi. I'm trying to create a small script for personal use. The scope is to upload automatically a certain txt file from my PC hard drive to my server (in a certain directory) every time the script is running. I try different ways (ftp upload using "ftp_put" command, trying to "get_file_contents", etc.) but i always have the same problem: the error i get is that "No such file or directory". This error is about the file on my local PC hard drive. I know that most browser for security reasons doesn't allow access to files from local PC hard drive. Is this the reason i fail miserably or there is a mistake when a declare the local (PC hard drive) path. Until now i try: c:\directory\subdurectory\filename.txt c:/directory/subdurectory/filename.txt file:///c:\directory\subdurectory\filename.txt file:///c:/directory/subdurectory/filename.txt every time without succes. Any ideas? Thank you in advance Quote Link to comment https://forums.phpfreaks.com/topic/301269-upload-file-from-local-pc-to-server/ Share on other sites More sharing options...
requinix Posted May 27, 2016 Share Posted May 27, 2016 Use forward slashes (backslashes are fine too but you have to escape them in strings) and no "file://" stuff. What's your code? Quote Link to comment https://forums.phpfreaks.com/topic/301269-upload-file-from-local-pc-to-server/#findComment-1533279 Share on other sites More sharing options...
filoaman Posted May 27, 2016 Author Share Posted May 27, 2016 I try forward slashes with no success. here is line of code where i get the error ftp_put($ftp_conn,'fileNameOnServer.txt', 'D:/directory/fileName.txt', FTP_ASCII ); Quote Link to comment https://forums.phpfreaks.com/topic/301269-upload-file-from-local-pc-to-server/#findComment-1533281 Share on other sites More sharing options...
requinix Posted May 27, 2016 Share Posted May 27, 2016 Does PHP have access to that file? Can you filesize() it and such without problems? What if you put the file somewhere else? Quote Link to comment https://forums.phpfreaks.com/topic/301269-upload-file-from-local-pc-to-server/#findComment-1533286 Share on other sites More sharing options...
gizmola Posted May 28, 2016 Share Posted May 28, 2016 Are you writing a command line php program that you are running from a CMD shell? That would be the ideal way to do something like this. If not you would have to be running within the environment of some sort of webserver, and that is not what you want to do for an automation utility. This page covers the basics of this: http://php.net/manual/en/install.windows.commandline.php You want to specify the path to the php interpreter in your windows and the path to your script, for example: C:\PHP5\php.exe -f "drive:/path/to/your/script.php" Once this runs natively, you can tweak your windows environment if you want to associate command line php with .php file extension scripts etc. if you choose. Quote Link to comment https://forums.phpfreaks.com/topic/301269-upload-file-from-local-pc-to-server/#findComment-1533288 Share on other sites More sharing options...
filoaman Posted May 28, 2016 Author Share Posted May 28, 2016 Thank you for your answers. @requinix No. I try to check the file size and i get error: "filesize(): stat failed" I try to run a script on my server from a browser, so maybe the problem is what i thought in the first place. I read somewhere that browsers block the access on local files, when you try to access them from a remote server (the internet) @gizmola As i mention above i try to run the script located on my internet server via a browser. I don't run the script on my computer so the source you mention in not relevant in my case. Quote Link to comment https://forums.phpfreaks.com/topic/301269-upload-file-from-local-pc-to-server/#findComment-1533291 Share on other sites More sharing options...
requinix Posted May 28, 2016 Share Posted May 28, 2016 Browser? Are you using IIS to run it? Quote Link to comment https://forums.phpfreaks.com/topic/301269-upload-file-from-local-pc-to-server/#findComment-1533292 Share on other sites More sharing options...
filoaman Posted May 28, 2016 Author Share Posted May 28, 2016 I'm referring to WEB BROWSER!!!!!!! I have a small script on my server (on the internet - on the cloud) and i'm trying to run the script from a WEB BROWSER! Quote Link to comment https://forums.phpfreaks.com/topic/301269-upload-file-from-local-pc-to-server/#findComment-1533293 Share on other sites More sharing options...
mac_gyver Posted May 28, 2016 Share Posted May 28, 2016 you cannot automatically upload a file from the client/local pc to a remote server using a script running on the remote server, without having some software running on the client/local pc. the script running on the remote server does not have any access to the files on the client/local pc, either through the browser or via any communication protocol. to do this, you first need to pick a protocol. ftp, http, or ssh would be the available choices. you would then need to install server software on the client/local pc, either an ftp server, a http server, or an ssh server. you could then have a script running on the remote server connect to the client/local pc and upload/copy the file. Quote Link to comment https://forums.phpfreaks.com/topic/301269-upload-file-from-local-pc-to-server/#findComment-1533294 Share on other sites More sharing options...
filoaman Posted May 28, 2016 Author Share Posted May 28, 2016 @ mac_gyver Thank you for your answer. Everything is clear now... Quote Link to comment https://forums.phpfreaks.com/topic/301269-upload-file-from-local-pc-to-server/#findComment-1533295 Share on other sites More sharing options...
mac_gyver Posted May 28, 2016 Share Posted May 28, 2016 (edited) actually, there may (untested) be a way to do this through the browser. at least the chrome browser, has a command line parameter that allows local file access. it's intended to allow local testing, where things like an ajax request would make use of a local file. when used, this applies to any url the browser requests, so you would only want to do this for your site, so as to not allow any other site access to files on the local pc. you would make a desk-top short-cut that invokes the browser with the correct command line parameter and only use this when visiting your site. i don't know if the 'same origin' policy would override this, since the web page is coming from a different address than the local usage in the code. assuming all this actually works, the client-side code on the page could make an ajax request to read the local file, then submit the data to the remote server in a post request. this sounds like a good phpfreaks programming challenge to see if anyone can come up with working code and for which browsers it would work. Edited May 28, 2016 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/301269-upload-file-from-local-pc-to-server/#findComment-1533296 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.