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

时间:2017-08-11 07:49:15

标签: php laravel

有些日子我已经被存放了。我从StackOverFlow的其他用户那里得到了帮助,但我们无法解决这个问题。

问题是我在'storage/public/projects/{$slug->name}'

上有文件夹

我有一个可以编辑项目的表单,表单如下:

 <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>

这个想法是当我更改slug并且不在输入类型文件上选择新图像时,我创建了一个新文件夹,删除了具有其他名称的最后一个文件夹并复制了这些图像(这两个图像我没有在表单上选择,但我有数据库。)

我们尝试这个代码并没有任何作用:

代码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();
}

代码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); 
        }

代码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); 
        }

0 个答案:

没有答案