I have a windows share created using samba .

It is located in my ubunutu server /mnt/user_new I want to copy files from /var/www/myproject/files/ to /mnt/user_new/files . This is for backup my files . and delete the original files .

I am using PHP . form PHP i tried

$file = '/var/www/html/bmw/myproject/files/0000000.pdf';
$newfile = '/mnt/user_new/0000000.pdf';

if (!copy($file, $newfile)) {
        echo "failed to copy $file...\n";

But i get a permission denied error .

Severity: Warning

Message: copy(/mnt/user_new/0000000.pdf) [function.copy]: failed to open stream: Permission denied

Is there any way to copy files from my Linux server to windows share . I have a username and password for access window share , but i don't know how to use it here . is there any way i can pass username and password .

To be honest this is my first time working with Samba , and the windows share is created by Network administrator . I have spent nearly 8 hours for this simple copy . but could not do .

Please help me . thanks in advance .

  • Can you access the share from the shell? Which command did you use to mount the share on your Linux box?
    – didierc
    Feb 11 '15 at 2:04
  • @didierc as i told in my question , mount was done by a previous system admin :( Feb 11 '15 at 2:06
  • And my first question?
    – didierc
    Feb 11 '15 at 2:07
  • 1
    À simple "solution" is to use the smbclient program to access the share using a smbfs style path to the share and username/password as parameter. This doesn't use the mount point installed by your predecessor. From php, you may use the system function to execute a shell command.
    – didierc
    Feb 11 '15 at 2:15
  • 1
    – didierc
    Feb 11 '15 at 2:19

So, your Linux server has a windows share mounted on a local directory, but apparently you cannot write to it. I suppose that this share has been setup as guest access, hence the read only status. Please refer to the ubuntu documentation on mounting windows share as a starting point to fix this configuration issue.

As a work around for the time being, I suggest using the smbclient from the command line or a shell script to perform your backups. This command can help you find out the path of the windows share if necessary:

$ smbclient -L <hostname>

but as I suggested in the comments, it might be simpler to just look it up from a FS browser (Gnome, KDE, or even a windows shell). This program acts more or less like a ftp client on top of the SMB/CIFS protocols, but it requires precise parameters, so you will have to investigate. For instance, if your windows server belongs to an active directory domain, you will have to pass the -k parameter to smbclient to let it know. You will also have to pass username and password along on the command line. There are many other parameters discussed on the man page, which could be important depending on your situation (eg. Do you need to encrypt the connection?).

In interactive mode, as mentioned earlier, this program works like a ftp client, so if you are familiar with ftp commands, you should feel at home with this tool. It is also possible to pass a sequence of ftp instructions on the command line, which is probably what you'll want to use to automate the backups until the share mount is fixed.

Finally, I also mentioned the system php function which comes handy when needing to invoke a shell command from a php script. Again, all this should be used a temporary solution until the share is fixed: you will have to write your credentials somewhere in the scripts or in a file configuration file accessible from smbclient, which is a bad practice. If possible, you should try to make manual backups, where your credentials would be provided interactively.

  • successfully connected to the windows share , had to use username and password also , now trying to copy files from Ubuntu to windows ;) . Thank you very much . Feb 11 '15 at 3:19
  • 1
    No problem, please don't hesitate to ask other questions if needed.
    – didierc
    Feb 11 '15 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.