douliu8327
2017-08-11 07:49
浏览 156
已采纳

Laravel:创建一个新文件夹并复制内容

I've been stocked some days with this. I get help from other user of StackOverFlow but we can't solve that.

The problem is I have folders on 'storage/public/projects/{$slug->name}'

I have a form where I can edit my projects, the form looks like this:

 <div class="form-group">
            <label><strong>Slug</strong></label>
              <textarea type="text" class="form-control form-control-sm" id="slug" name="slug" rows="1" required style="resize:none;">{{$project->slug}}</textarea>
          </div>

              <div class="form-group">
                  <label><strong>Order</strong></label>
                  <textarea type="number" class="form-control form-control-sm" id="order" name="order" rows="1">{{$project->order}}</textarea>
            </div>

        <div class="form-group">
            <label><strong>Public</strong></label>
            <textarea type="text" class="form-control form-control-sm" id="public" name="public" value="" rows="1">{{$project->public}}</textarea>
        </div>

        <div class="form-group">
       <table>
            <tr>
              <td>
                  @if (Storage::disk('projects')->has($project->slug))
                  <img src="{{ asset('/storage/projects/'.$project->slug.'/header.jpg') }}" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
                  @else
                  <img src="" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
                  @endif
                  <input type="file" name="pathheader" id="pathheader" class="form-control-file" aria-describedby="fileHelp" style="display:none;">
              </td>
              <td>
                  @if (Storage::disk('projects')->has($project->slug))
                  <img src="{{ asset('/storage/projects/'.$project->slug.'/home.jpg') }}" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
                  @else
                  <img src="" id="img2" class="img2" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
                  @endif
                  <input type="file" name="pathhome" id="pathhome" class="form-control-file" aria-describedby="fileHelp" style="display:none;"><br>
              </td>
            </tr>
            <tr>
              <td>
                  <input type="button" name="" value="Seleccionar header" id="browse_file" class="btn btn-danger form-control">
              </td>
              <td>
                  <input type="button" name="" value="Seleccionar home" id="browse_file2" class="btn btn-danger form-control">
              </td>
            </tr>
        </table>
        <br><br>
        </div>

The idea was when I change the slug and don't select new images on the input type file, I create a new folder, delete the last folder with the other name and copy the containt (the two images i don't selected on the form, but I have on data base).

We try it this codes and nothing works:

CODE 1

public function updateProject(Request $request, $id) //Actualizar la informacion de un trabajador 
{ 
$project = Project::find($id); 
if ($request->has('slug')){ 
\Storage::disk('projects')->deleteDirectory($project->slug); 
} 
$project->slug = $request->input('slug'); 
\Storage::disk('projects')->makeDirectory($project->slug); 

$project->order = $request->input('order'); 
$project->public = $request->input('public'); 
if($request->hasFile('pathheader')){ 
$project->pathheader = $request->file('pathheader'); 
\Storage::disk('projects')->putFileAs($project->slug,$project->pathheader,'header.jpg'); 
} 
$project->pathheader = Project::find($id)->pathheader; 
\Storage::disk('projects')->putFileAs($project->slug,$project->pathheader,'header.jpg'); 

if($request->hasFile('pathhome')){ 
$project->pathhome = $request->file('pathhome'); 
\Storage::disk('projects')->putFileAs($project->slug,$project->pathhome,'home.jpg'); 
} 
$project->pathhome = Project::find($id)->pathhome; 
\Storage::disk('projects')->putFileAs($project->slug,$project->pathhome,'home.jpg'); 

$project->save();
}

CODE 2

$project = Project::find($id); 

        $oldSlug = $project->slug; 

        if (strcmp($oldSlug, $request->input('slug')) !== 0) { 
        $project->slug = $request->input('slug'); 
        \Storage::disk('projects')->makeDirectory($project->slug); 
        } 

        $project->order = $request->input('order'); 
        $project->public = $request->input('public'); 

        if($request->hasFile('pathheader')){ 
        $project->pathheader = $request->file('pathheader'); 
        \Storage::disk('projects')->putFileAs($project->slug,$project->pathheader,'header.jpg'); 
        } 
        $project->pathheader = Project::find($id)->pathheader; 
        \Storage::disk('projects')->putFileAs($project->slug,$project->pathheader,'header.jpg'); 

        if($request->hasFile('pathhome')){ 
        $project->pathhome = $request->file('pathhome'); 
        \Storage::disk('projects')->putFileAs($project->slug,$project->pathhome,'home.jpg'); 
        } 
        $project->pathhome = Project::find($id)->pathhome; 
        \Storage::disk('projects')->putFileAs($project->slug,$project->pathhome,'home.jpg'); 

        $project->save(); 

        if (strcmp($oldSlug, $request->input('slug')) !== 0) { 
        \Storage::disk('projects')->deleteDirectory($oldSlug); 
        }

CODE 3

 $project = Project::find($id); 

        $oldSlug = $project->slug; 

        if (strcmp($oldSlug, $request->input('slug')) !== 0) { 
        $project->slug = $request->input('slug'); 
        \Storage::disk('projects')->makeDirectory($project->slug); 
        } 

        $project->order = $request->input('order'); 
        $project->public = $request->input('public'); 

        if($request->hasFile('pathheader')){ 
        $project->pathheader = \Storage::disk('projects')->putFileAs($project->slug,$request->file('pathheader'),'header.jpg'); 
        } 

        if($request->hasFile('pathhome')){ 
        $project->pathhome = \Storage::disk('projects')->putFileAs($project->slug,$request->file('pathhome'),'home.jpg'); 
        } 

        $project->save(); 

        if (strcmp($oldSlug, $request->input('slug')) !== 0) { 
        \Storage::disk('projects')->deleteDirectory($oldSlug); 
        }
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanhongxian6982 2017-08-11 12:09
    已采纳

    Try it with remaning the project folder :

    public function updateProject(Request $request, $id) {
        $project = Project::find($id);
    
        $project->order = $request->input('order');
        $project->public = $request->input('public');
    
        if (strcmp($project->slug, $request->input('slug')) !== 0) {
            Storage::disk('projects')->move($project->slug, $request->input('slug')); // rename the project folder
            $project->pathheader = $request->input('slug').'/header.jpg'; //if it's always the image will be named header.jpg else substr($project->pathheader, strlen($project->slug));
            $project->pathhome = $request->input('slug').'/home.jpg'; //if it's always the image will be named home.jpg else substr($project->pathhome, strlen($project->slug));
            $project->slug = $request->input('slug');
        }
    
    
        if($request->hasFile('pathheader')){
        $project->pathheader = \Storage::disk('projects')->putFileAs($project->slug,$request->file('pathheader'),'header.jpg');
        }
    
        if($request->hasFile('pathhome')){
        $project->pathhome = \Storage::disk('projects')->putFileAs($project->slug,$request->file('pathhome'),'home.jpg');
        }
    
        $project->save();
    
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题